button mode switcher not working

I’ve been working on a smart watch project but the code for the button isn’t working :frowning: .
does anyone have any ideas why it isn’t working?
here’s the code:

int menu = 0;
const int switchPin = 4;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

int sec = 00;
int min = 00;
int hr = 00;
int stoprounds = 0;
int mark;

#include <SPI.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#include <DHT_U.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define LOGO16_Gdisplay_HEIGHT 16 
#define LOGO16_Gdisplay_WIDTH  16 

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

#define DHTPIN 6     //digital pin sensor

// Uncomment whatever type you're using!

#define DHTTYPE DHT11      // DHT 11
 

DHT dht(DHTPIN, DHTTYPE);





const char *monthName[12] = {  //set time
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
   mark = 0;
  
   bool parse=false;//set time
  bool config=false;//set time

  if (getDate(__DATE__) && getTime(__TIME__)) {//set time
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
  delay(200);
  /*display.setCursor(0,0);
  display.print("DS1307RTC Read Test");
  display.setCursor(1,0);
  display.print("-------------------");
  */

}

void loop() {
  // put your main code here, to run repeatedly:
  tmElements_t tm;
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    // if the state has changed from HIGH to LOW
    // you know that the ball has been tilted from
    // one direction to the other
    if (switchState == LOW) {
      menu++;
    }
  }
  Serial.println(menu);
  if (menu == 0) {
    dateandtime();
  }
  if (menu == 1) {
    stopwatch();
  }
  if (menu == 2) {
    timer();
  }
  if (menu == 3) {
    alarm();
  }
  if (menu == 4) {
    weather();
  }
  if (menu == 5) {
    compass();
  }
  if (menu == 6) {
    menu = 0;
    delay(500);
  }
}
void dateandtime() {
  if (RTC.read(tm)) {
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.print(tm.Day);
    display.write('/');
    display.print(tm.Month);
    display.write('/');
    display.print(tmYearToCalendar(tm.Year));
    display.setCursor(10,15);
    display.setTextSize(2);
    print2digits(tm.Hour);
    //display.display();
    display.write(':');
    print2digits(tm.Minute);
    //display.display();
    display.write(':');
    display.setTextSize(1);
    print2digits(tm.Second);
    //display.display();
    //display.print("Date= ");
    display.clearDisplay();
    
  } else {
    if (RTC.chipPresent()) {
      display.clearDisplay();
      display.print("clock");
      display.print("stoped");
      display.display();
      
    } else {
      display.clearDisplay();
      display.print("fault");
      display.display();
      
    }
    delay(9000);
  }
  delay(1000);

}
void stopwatch() {
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(3,15);
 

 
  display.print(hr);
  display.print(":");
  display.print(min);
  display.print(":");
  display.println(sec);
  display.setCursor(100,3);
  display.setTextSize(1);
  display.println(stoprounds);
  display.display();
  sec = sec + 1;
  delay(1000);


  if (sec == 60) {
    sec = 00;
    min = min + 1;
  } else;
  if (min == 60) {
    min = 0;
    hr = hr + 1;
  }
  if (hr == 24) {
    hr = 00;
    min = 00;
    sec = 00;
    stoprounds = stoprounds + 1;
  }
  if (stoprounds = 9) {
    stoprounds = 0;
  }

}
void timer() {
  display.setCursor(0,0);
  display.print("timer comming soon");
  display.display();

}
void alarm() {
  display.setCursor(0,0);
  display.print("alarm comming soon");
  display.display();

}
void weather() {
  delay(500);//?//dht -->
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.print("NO CONNECTION   ");
    return; 
  }
  display.display();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print(h);
  display.print(" %   ");
  display.print(t);
  display.print(" C");
  display.clearDisplay();

}
void compass() {
  display.setCursor(0,0);
  display.print("compass comming soon");
  display.display();

  

}
bool getTime(const char *str)//--> set time
{                           
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}









void print2digits(int number) {
  if (number >= 0 && number < 10) {
    display.write('0');
    display.display();
  }
  display.print(number);
  display.display();
}

Do you have a pullup or pulldown resistor on your switch pin? Using INPUT_PULLUP achieves the same purpose.

Allan

You never update prevSwitchState

Add prevSwitchState = switchState in the switch testing section.

Stop using the delay() function as it freezes code execution for that amount of time.

like this(lines 87 - 123)?:

void loop() {
  // put your main code here, to run repeatedly:
  tmElements_t tm;
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    // if the state has changed from HIGH to LOW
    // you know that the ball has been tilted from
    // one direction to the other
    if (switchState == LOW) {
      menu++;
    }
  }
  Serial.println(menu);
  if (menu == 0) {
    dateandtime();
  }
  if (menu == 1) {
    stopwatch();
  }
  if (menu == 2) {
    timer();
  }
  if (menu == 3) {
    alarm();
  }
  if (menu == 4) {
    weather();
  }
  if (menu == 5) {
    compass();
  }
  if (menu == 6) {
    menu = 0;
    delay(500);
  }
  prevSwitchState = switchState;
}
void loop() {
  // put your main code here, to run repeatedly:
  tmElements_t tm;
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    // if the state has changed from HIGH to LOW
    // you know that the ball has been tilted from
    // one direction to the other

    prevSwitchState = switchState;

    if (switchState == LOW) {
      menu++;
    }
  }
  Serial.println(menu);
  if (menu == 0) {
    dateandtime();
  }
  if (menu == 1) {
    stopwatch();
  }
  if (menu == 2) {
    timer();
  }
  if (menu == 3) {
    alarm();
  }
  if (menu == 4) {
    weather();
  }
  if (menu == 5) {
    compass();
  }
  if (menu == 6) {
    menu = 0;
    delay(500);
  }

}

the mode is locked 0 even though I tried you’re code.

but thanks for you’re help

the screen (ssd1306) isn't working either :frowning:

I think it's time you posted a full circuit diagram.

Not Fritzing, PLEASE.

Allan

What program would you recomend?

I've got tinyCad and kiCad, but they don't have any arduino components.

Old school paper drawn is fine; snap a pic with your phone and upload. Fortunately, they’re not tech snobs and will work with your diagram.

KiCAD should have Arduino bits available but I usually just draw a standard connector for the Arduino pins (why bother with all that clutter of a complete Arduino image when really all you need is some I/O pins?).

I don't know tinyCAD.

Pen and paper also works just fine in most cases.