Sunday 10 June 2018

Basic Master clock with OLED display using delay function

PinWiring to Arduino Uno
Vin5V
GNDGND
SCLA5
SDAA4
 
This uses a combination of a few blog posts
 
The POT is wired thus:
centre (wiper) to A0
Right to GND
Left to 5V
 
The LED
Cathode to gnd via a 220 ohm resistor
Anode to pin 8
 
The delay function is essentially the clock.
Works ok, though using the delay() means that nothing else can run until the delay has finished.

The LED is on/off for a total of 60,000 millisecs/BPM  = 1min/BPM
and then loops.   
 

 
 
The code is here:
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define LED 8      // LED pin

#define MIN_BPM 20      /*write here the min BPM that you want */
 #define MAX_BPM 300     /* write here the max BPM that you want */
 #define POT A0          // the potentiometer connects to analog pin A0

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Variables
int bpm;

void setup() {
 
  pinMode(LED, OUTPUT);  // LED pin 8 is the output
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C
  display.clearDisplay(); // Clear the buffer.
 
}
//---------------------------------------------------------------

void loop() {

    bpm = map(analogRead(POT), 0, 1023, MIN_BPM, MAX_BPM);  
    display.clearDisplay();
    display.setTextSize(3);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println(bpm);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.println("    BPM");
    display.display();

// the LED is on/off for a total of 60,000 millisecs/BPM  = 1min/BPM
// and then loops. 

/*this is the 1/1 output*/
  digitalWrite(LED, HIGH); // turn LED on for 2 millisecs
    delay(2000/bpm);
    digitalWrite(LED, LOW); // turn LED off
    delay(58000 / bpm); 
 


 }

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

No comments:

Post a Comment