Debreczeni Tamas
Published © GPL3+

Arduino Intervalometer

You want to make a quality timelapse, you need one.

BeginnerFull instructions provided2 hours672
Arduino Intervalometer

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1
Slide Switch
Slide Switch
×1
0.96" OLED 64x128 Display Module
ElectroPeak 0.96" OLED 64x128 Display Module
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Arduino Intervalometer

Code

Arduino Intervalometer 1.1

Arduino
the code is really messy, sorry for that
//OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <Fonts/FreeSerif12pt7b.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels ( it shoud be 64 )
#define OLED_RESET 5
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// 'Untitled-2', 128x64px
const unsigned char myBitmap [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xdf, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0xfe, 0xff, 
  0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0xfe, 0xff, 
  0xff, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xfe, 0x7f, 
  0xff, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0x7f, 
  0xff, 0x9f, 0xff, 0xc0, 0x00, 0x1f, 0x9f, 0xff, 0xff, 0xff, 0x9f, 0x80, 0x00, 0x7f, 0xff, 0x7f, 
  0xff, 0x9f, 0xf0, 0x00, 0x00, 0x01, 0x9f, 0xff, 0xff, 0xff, 0x38, 0x00, 0x00, 0x01, 0xff, 0x7f, 
  0xff, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x7f, 
  0xff, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x07, 0x3f, 
  0xff, 0x20, 0x03, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x07, 0xf0, 0x01, 0x3f, 
  0xff, 0x00, 0x7f, 0xff, 0xc0, 0x00, 0x03, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x3f, 
  0xff, 0x03, 0xff, 0xff, 0xf0, 0x00, 0x01, 0xff, 0xff, 0xf8, 0x00, 0x01, 0xff, 0xff, 0xf0, 0x3f, 
  0xff, 0x0f, 0xff, 0xff, 0xfc, 0x00, 0x3c, 0xff, 0xff, 0xf3, 0xc0, 0x07, 0xff, 0xff, 0xfc, 0x3f, 
  0xff, 0x1f, 0xff, 0xff, 0xfe, 0x00, 0xfc, 0xff, 0xff, 0xe7, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0x3f, 
  0xff, 0x7f, 0xff, 0xff, 0xff, 0x01, 0xfe, 0x7f, 0xff, 0xe7, 0xf0, 0x1f, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x7f, 0xff, 0xff, 0xff, 0x83, 0xff, 0x7f, 0xff, 0xcf, 0xf8, 0x3f, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x7f, 0xff, 0xff, 0xff, 0x83, 0xff, 0x3f, 0xff, 0xcf, 0xfc, 0x3f, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x7f, 0xff, 0xff, 0xff, 0xc7, 0xff, 0x3f, 0xff, 0xdf, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xbf, 0xff, 0x9f, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xff, 0xff, 0xef, 0xff, 0x9f, 0xff, 0x9f, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xff, 0xff, 0xef, 0xff, 0x9f, 0xff, 0x9f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xef, 0xff, 0x9f, 0xff, 0x9f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xef, 0xff, 0x9f, 0xff, 0x9f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xef, 0xff, 0x9f, 0xff, 0xbf, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xef, 0xff, 0x9f, 0xff, 0xbf, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xcf, 0xff, 0x9f, 0xff, 0x9f, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xcf, 0xff, 0x9f, 0xff, 0x9f, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xcf, 0xff, 0x9f, 0xff, 0x9f, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xff, 0x9f, 0xff, 0x9f, 0xff, 0x3f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xbf, 0xff, 0x9f, 0xff, 0x3f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xbf, 0xff, 0x3f, 0xff, 0xdf, 0xff, 0x9f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0x3f, 0xff, 0x3f, 0xff, 0xcf, 0xff, 0x9f, 0xff, 0xff, 0xff, 0x9f, 
  0xfe, 0x7f, 0xff, 0xff, 0xfe, 0x7f, 0xfe, 0x7f, 0xff, 0xef, 0xff, 0xcf, 0xff, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xff, 0xfc, 0xff, 0xfe, 0x7f, 0xff, 0xe7, 0xff, 0xe3, 0xff, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xff, 0xf0, 0x3f, 0xfc, 0xff, 0xff, 0xf3, 0xff, 0x81, 0xff, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xff, 0x80, 0x01, 0xf9, 0xff, 0xff, 0xf3, 0xf8, 0x00, 0x3f, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xf8, 0x19, 0xfc, 0x73, 0xff, 0xff, 0xf9, 0xc3, 0xf3, 0x07, 0xff, 0xff, 0x9f, 
  0xff, 0x7f, 0xff, 0xff, 0xf9, 0xff, 0xf3, 0xff, 0xff, 0xfc, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x3f, 0xff, 0xff, 0xf9, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x3f, 0xff, 0xff, 0xf9, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x3f, 0xff, 0xff, 0xf9, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x3f, 0xff, 0xff, 0xf9, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xbf, 
  0xff, 0x3f, 0xff, 0xff, 0xfb, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0xff, 0xfb, 0xff, 0xff, 0xff, 0x3f, 
  0xff, 0x3f, 0xff, 0xff, 0xfb, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0xff, 0xfb, 0xff, 0xff, 0xff, 0x3f, 
  0xff, 0xbf, 0xff, 0xff, 0xfb, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0xff, 0xfb, 0xff, 0xff, 0xff, 0x3f, 
  0xff, 0xbf, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xfb, 0xff, 0xff, 0xff, 0x7f, 
  0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0x7f, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};


// 'BatteryIndicator 14x7', 14x7px
const unsigned char BatteryIndicator [] PROGMEM = {
  0xff, 0xf8, 0x80, 0x08, 0x80, 0x0c, 0x80, 0x0c, 0x80, 0x0c, 0x80, 0x08, 0xff, 0xf8
};




const int Led = 13;
const int pin_A = 3;                       // rotary switch CLK
const int pin_B = 4;                       // rotary switch DT
const int Rotbutton = 5;                   // rotary switch SW (push button)
const int Switch = 6;                      // toggle switch to start
const int Trigger = 7;                     // trigger PIN
const int Trigger2 = 8;
const int Voltage = A3;                    // analog pin for the battery voltage

int  PicsTaken       = 0;                   // Photos taken
int  ValToSet        = 5;                   // Val used with the rotary switch (Interval on start)
long Interval        = 0;                   // Interval
long Durration       = 15;                  // Final time lapse video duration
int  Incremvalue     = 1;                   // How many +/- to the ValToSet
int  PicsNeed        = 0;                   // Pictures needed for video duration
long VideoFR         = 30;                  // Time lapse video frame rate
long Record          = 0;                   // Neded images record time
int  loading         = 0;                   // Value for the loading animation
int  Speed           = 0;                   // Time Speed rate
int  RemainingPics   = 0;                   // Remaining pictures until the end of the time lapse
long TimeLeft        = 0;                   // Time left until the end of the time lapse
int  BatteryV        = 0;                   // Battery voltage value
int  BatteryPer      = 0;
int b;

unsigned long currentTime;
unsigned long loopTime;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev = 0;

int prevButtonState = LOW;
int buttonState = LOW;


void setup()  {
  Serial.begin(9600);

  //OLED Display
  Wire.begin();
  Wire.setClock(400000);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  pinMode(Led, OUTPUT);
  pinMode(Trigger2, OUTPUT);
  pinMode(Trigger, OUTPUT);
  pinMode(pin_A, INPUT);
  pinMode(pin_B, INPUT);
  pinMode(Rotbutton, INPUT);
  pinMode(Switch, INPUT);
  pinMode(Voltage, INPUT);

  currentTime = millis();
  loopTime = currentTime;

  digitalWrite(Trigger, LOW);

  display.clearDisplay();
  display.drawBitmap(0, 0,  myBitmap, 128, 64, WHITE);
  display.display();

}
void loop()  { 

   buttonState = digitalRead(Rotbutton);  

   // IF switch is OFF
  
  if (digitalRead(Switch) == LOW) {

    // get the current elapsed time
    currentTime = millis();

    if (currentTime >= (loopTime + 5)) {                                // 5ms since last check of encoder = 200Hz
      encoder_A = digitalRead(pin_A);                                   // Read encoder pins
      encoder_B = digitalRead(pin_B);
      if ((!encoder_A) && (encoder_A_prev)) {                           // A has gone from high to low
        if (encoder_B) {                                                // B is high so clockwise
          if (ValToSet + Incremvalue <= 255) ValToSet += Incremvalue;   // increase the Interval, don't go over 255
          tone(Trigger2, 1000); // Send  sound signal...

        }
        else {                                                          // B is low so counter-clockwise
          if (ValToSet - Incremvalue >= 1) ValToSet -= Incremvalue;     // decrease the Interval, dont go below 1
          tone(Trigger2, 1000); // Send  sound signal...

        }

        noTone(Trigger2);      // Stop sound...


        if (buttonState == HIGH) {
          Interval = ValToSet;
        } else {
          Durration = ValToSet;
        }



        prevButtonState = buttonState;

        PicsNeed = Durration * VideoFR;
        Record = (Durration * VideoFR * Interval) / 60 ;
        Speed = (Record*60)/ Durration;
        BatteryV = analogRead(Voltage);
        BatteryV = map(BatteryV, 550, 846, 0, 11 ); 
        b = analogRead(Voltage);
        b = map(b, 550, 846, 0, 20 ); 
        BatteryPer = b;
        BatteryPer = map(BatteryPer, 0, 20, 0, 100 );
//    BatteryPer = analogRead(Voltage);
//    BatteryPer = map(BatteryPer, 550, 846, 0, 100 );

        //  display.setFont(&FreeSerif12pt7b);
        display.clearDisplay();
        display.setTextColor(WHITE);
        display.setTextSize(1);

        display.drawRect(0, 0, display.width(), display.height(), WHITE);
        display.drawLine(0, 10, display.width(), 10, WHITE);      //display.width() (128)
   
        display.setCursor(85, 2);                                                                       // BATTERY
        display.print(String(BatteryPer) + "%");        
        display.drawBitmap(110, 2,  BatteryIndicator, 14, 7, WHITE);
        display.fillRect(111, 3, BatteryV, 5, WHITE);



        display.setCursor(8, 15);
        display.print("Interval");
        display.setCursor(70, 15);
        display.print("= " + String(Interval) + " s");

        display.setCursor(8, 27);
        display.print("Vid.Dur.");
        display.setCursor(70, 27);
        display.print("= " + String(Durration) + " s");

        display.setCursor(8, 38);
        display.print("Pics.N.");
        display.setCursor(70, 38);
        display.print("= " + String(PicsNeed));

      /*  display.setCursor(8, 38);
        display.print("Speed");
        display.setCursor(70, 38);
        display.print("= " + String(Speed) + " x");

      */ display.drawLine(6, 48, 122, 48, WHITE);


        display.setCursor(8, 52);
        display.print("Record");
        display.setCursor(70, 52);
        display.print("= " + String(Record) + " min");


   


        display.display();


      }
      encoder_A_prev = encoder_A;         // Store value of A for next time

      loopTime = currentTime;             // Updates loopTime
      PicsTaken = 0;

    }
    
// IF switch is ON

} else {

  if ( PicsTaken < PicsNeed) {

    PicsTaken += 1;
    loading = PicsTaken;
    RemainingPics = PicsNeed - PicsTaken;
    TimeLeft = (RemainingPics*Interval)/60;
    BatteryV = analogRead(Voltage);
    BatteryV = map(BatteryV, 550, 846, 0, 11 ); 
    b = analogRead(Voltage);
    b = map(b, 550, 846, 0, 20 ); 
    BatteryPer = b;
    BatteryPer = map(BatteryPer, 0, 20, 0, 100 );
// BatteryPer = analogRead(Voltage);
// BatteryPer = map(BatteryPer, 550, 846, 0, 100 );

    loading = map(loading, 0, PicsNeed, 0, display.width());

    display.clearDisplay();
    display.setTextSize(1);

    display.drawRect(0, 0, display.width(), display.height(), WHITE);
    display.drawLine(0, 10, display.width(), 10, WHITE);      //display.width() (128)

    display.drawRect(0, 42, display.width(), 8, WHITE);        
    display.fillRect(0, 42, loading, 8, WHITE);
    
    display.setTextSize(2);
    display.setCursor(58, 20);
    display.print("ON");
    display.fillCircle(46, 26, 5, WHITE);

    display.setTextSize(1);   
    display.setCursor(2, 2);
    display.print("Int:" + String(Interval) + "s");
    display.setCursor(30, 0);

   
    
    //display.setCursor(78, 2);
    // display.print("Rec:" + String(Record) + "m"); 

    display.setCursor(4, 34);                                                   
    display.print(PicsTaken);
    display.setCursor(107, 34);                                                   
    display.print(PicsNeed);


    
    display.setCursor(4, 53);
    display.print("T.left:  " + String(TimeLeft) + " min");

//     display.setCursor(102, 34);                                                  // BATTERY
//     display.print(analogRead(Voltage));
     display.drawBitmap(110, 2,  BatteryIndicator, 14, 7, WHITE);
     display.fillRect(111, 3, BatteryV, 5, WHITE);     
     display.setCursor(85, 2);
     display.print(String(BatteryPer) + "%");   
     display.display();

// Serial.print (loading);

    delay(700);                        // delay before the first pics
    digitalWrite(Trigger, HIGH);
    
/*    delay(100);                           // if when the camera is in standby mode
    digitalWrite(Trigger, LOW);
    delay(200);
    digitalWrite(Trigger, HIGH);
*/ 
    digitalWrite(Led, HIGH);   // turn the LED on
    delay(200);                        // delay for the camera button press
    digitalWrite(Trigger, LOW);
    digitalWrite(Led, LOW);    // turn the LED off
    delay((1000 * Interval) - 880);    // delay befor next pics - 900 (delay before the pics + camera button press)

// DONE

  } else {

    digitalWrite(Trigger, LOW);
    digitalWrite(Led, HIGH);

    display.clearDisplay();
 // display.drawBitmap(0, 0,  myBitmap, 128, 64, WHITE);
 
    display.drawRect(0, 0, display.width(), display.height(), WHITE);
    display.drawLine(0, 10, display.width(), 10, WHITE);
    display.drawLine(0, 50, display.width(), 50, WHITE);
    
    display.setCursor(30, 20);
    display.setTextSize(3);
    display.println("Done");    
    

    display.setTextSize(1);
    display.setCursor(2, 2);
    display.print("Int:" + String(Interval) + "s");
    display.setCursor(78, 2);
    display.print("Rec:" + String(Record) + "m");

    display.setCursor(8, 53);
    display.print("Pics: " + String(PicsTaken));
    display.setCursor(80, 53);
    display.print("of " + String(PicsNeed));

    display.display();



  }
}



}

Arduino Intervalometer

Arduino
//OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);

const int pin_A = 3;                      // rotary switch CLK
const int pin_B = 4;                      // rotary switch DT
const int Rotbutton = 5;                   // rotary switch SW (push button) 
const int Switch = 6;                      // toggle switch to start
const int Trigger = 7;                     // trigger PIN

float Interval = 3;                        // Interval on start
float Incremvalue = 1.0;                   // how many +/- to the interval
unsigned long currentTime;
unsigned long loopTime;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev = 0;


void setup()  {
  Serial.begin(9600);

  //OLED Display
  Wire.begin();
  Wire.setClock(400000);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);


  pinMode(Trigger, OUTPUT);
  pinMode(pin_A, INPUT);
  pinMode(pin_B, INPUT);
  pinMode(Rotbutton, INPUT);
  pinMode(Switch, INPUT);

  currentTime = millis();
  loopTime = currentTime;
  
  digitalWrite(Trigger, LOW);


}

void loop()  {

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.print(" Interval:  ");
 

  if (digitalRead(Switch) == LOW) {

    // get the current elapsed time
    currentTime = millis();
    if (currentTime >= (loopTime + 5)) {
      // 5ms since last check of encoder = 200Hz
      encoder_A = digitalRead(pin_A);    // Read encoder pins
      encoder_B = digitalRead(pin_B);
      if ((!encoder_A) && (encoder_A_prev)) {
        // A has gone from high to low
        if (encoder_B) {
          // B is high so clockwise
          // increase the Interval, dont go over 255
          if (Interval + Incremvalue <= 255) Interval += Incremvalue;
        }
        else {
          // B is low so counter-clockwise
          // decrease the Interval, dont go below 0
          if (Interval - Incremvalue >= 0) Interval -= Incremvalue;
        }
        
          display.print(Interval);
          display.print(" s");
          display.display();
        
      }
      encoder_A_prev = encoder_A;     // Store value of A for next time

      

      loopTime = currentTime;  // Updates loopTime

     if (digitalRead(Rotbutton) == LOW) {
         digitalWrite(Trigger, HIGH);
         digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)  
         delay(200);
         digitalWrite(Trigger, LOW);
         digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW  
         delay(200);
     }
    
    Serial.println(Interval);
    }

    


  } else {
    digitalWrite(Trigger, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)  
    delay(200);
    digitalWrite(Trigger, LOW);
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW  
    delay(1000 * Interval);
  }




}

Credits

Debreczeni Tamas

Debreczeni Tamas

6 projects • 20 followers

Comments