Need help with my arduino code

i need a code that will code my protoype oven in arduino sketch software it has three buttons, 1 buzzer and one led and an lcd
the code should do the following

  1. the lcd should not display anything until one of hte three buttons is pressed
  2. when one of the three buttons is pressed, the lcd should turn on saying "set timer"
  3. then the user will set it in minutes using pin up to increase in minutes and pin down to decrease in minutes, then the select is to start the timer
  4. when the timer finishes, the buzzer and light should go on and another countdown timer(one minute) to automatically shut off the oven((turn off the lcd) should start
  5. the buzzer and light should not turn off until a button is pressed during the auotmatic oven shut off or until the one miinute counter finishes
  6. if a button is pressed it should go back to the set timer screen but if the one minute counter finishes the lcd should clear and go off and the buzzer should go off too and the light
  7. miind you when i mean the buzzer and light should come on i want the light to be flashiing and the buzzer too

the code doesnt do what its supposed to do and i dont know whats wrong
here is the code

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
const int pin_up = A0; // the pin that the button is connected to
const int pin_down = A1;//pin that button 2 is connected to
const int pin_select = A2;//pin that button 3 is connected to 
const int buzzer = 8;//pin that buzzer is connected to 
const int light = 13;//pin that light is connected to

int state_up = 0; // variable for storing the button state
int state_down = 0;
int state_select = 0;
int OvenState = 0;
int minutes = 0; // initial value of the timer

void setup() {
  lcd.begin(16, 2); // set up the LCD's number of columns and rows
  pinMode(pin_up, INPUT); // set the button pin as input
  pinMode(pin_down, INPUT);
  pinMode(pin_select, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(light, OUTPUT);
}

void loop() {
  switch (OvenState) //switch case to determine oven state
  {
  case 0:
    if (digitalRead(pin_up) == HIGH || digitalRead(pin_down) == HIGH || digitalRead(pin_select) == HIGH) {
      lcd.clear();
      OvenState = 1;
    }
    break;

  case 1:
    if (digitalRead(pin_up) == HIGH) {
      delay(200);
      minutes++;
    }
    if (digitalRead(pin_down) == HIGH) {
      delay(200);
      minutes--;
      if (minutes < 0) {
        minutes = 0;
      }
    }
    lcd.clear(); 
    lcd.setCursor(0, 0);
    lcd.print("Set timer:");
    lcd.setCursor(0, 1);
    lcd.print(minutes);
    lcd.display();
    if (digitalRead(pin_select) == HIGH) {
      minutes *= 60;
      OvenState = 2;
      delay(200);
    }
    break;

  case 2:
  while (minutes > 0 && OvenState == 2) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Time remaining: ");
  lcd.setCursor(0, 1);
  lcd.print(minutes / 60);
  lcd.print(":");
  if (minutes % 60 < 10) {
    lcd.print("0");
  }
  lcd.print(minutes % 60);
  delay(1000);
  minutes--;

  if (minutes == 0) {
    lcd.clear();
    lcd.print("Times Up!");
    delay(2000);
    OvenState = 3;
    break;
  }

  if (digitalRead(pin_select) == HIGH)
   {
    lcd.clear();
    minutes = 0;
    OvenState = 1;
    delay(200);
    break;
  }
}
  case 3:
   if (OvenState == 3) {
  int seconds = 60;

  lcd.clear();
  lcd.print("Oven will shut off in: ");
  lcd.setCursor(0, 1);
  lcd.print("1:00");
  
  // Start buzzer and flashing light
  pinMode(buzzer, OUTPUT);
  pinMode(light, OUTPUT);
  digitalWrite(buzzer, LOW);
  digitalWrite(light, LOW);
  bool isBuzzerOn = false;
  bool isLightOn = false;
  unsigned long prevTime = millis();
  bool isButtonPressed = false;

  while (seconds > 0 && !isButtonPressed) {
    unsigned long currentTime = millis();
    if (currentTime - prevTime >= 500) {
      prevTime = currentTime;
      if (isBuzzerOn) {
        digitalWrite(buzzer, LOW);
        isBuzzerOn = false;
      } else {
        digitalWrite(buzzer, HIGH);
        isBuzzerOn = true;
      }

      if (isLightOn)
      {
        digitalWrite(light, LOW);
        isLightOn = false;
      } else {
        digitalWrite(light, HIGH);
        isLightOn = true;
      }
    }

    lcd.setCursor(0, 1);
    lcd.print("0:");
    if (seconds < 10)
    {
      lcd.print("0");
    }
    lcd.print(seconds);

    if (digitalRead(pin_select) == LOW) 
    {
      isButtonPressed = true;
    }
    
    delay(1000);
    seconds--;
  }

  // Turn off buzzer and light
  digitalWrite(buzzer, LOW);
  digitalWrite(light, LOW);
  pinMode(buzzer, INPUT);
  pinMode(light, INPUT);

  // Clear LCD and return to Case 1 if a button was pressed
  if (isButtonPressed) {
    lcd.clear();
    OvenState = 1;
  }
}
}
}

the buzzer doesnt go off the light doesnt flash when the timer hiits 0
and the automatic counter doesnt start at all idk why

Did you test all the hardware independently of your sketch, by using simple test sketches that only exercise one item of hardware at a time?

You can stare at code all day and it won't help if there is a hardware issue.

its not a hardware issue

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
const int pin_up = A0; // the pin that the button is connected to
const int pin_down = A1;//pin that button 2 is connected to
const int pin_select = A2;//pin that button 3 is connected to
const int pin_buzzer = 8;//pin that pin_buzzer is connected to
const int pin_light = 13;//pin that pin_light is connected to

bool state_up = 0; // variable for storing the button state
bool state_down = 0;
bool state_select = 0;
bool OvenState = 0;
byte minutes = 0; // initial value of the timer
byte seconds = 60;


void setup() {
  lcd.begin(16, 2); // set up the LCD's number of columns and rows
  pinMode(pin_up, INPUT); // set the button pin as input
  pinMode(pin_down, INPUT);
  pinMode(pin_select, INPUT);
  pinMode(pin_buzzer, OUTPUT);
  pinMode(pin_light, OUTPUT);
}

void loop() {
  switch (OvenState) { //switch case to determine oven state
    case 0:
      if (digitalRead(pin_up) && digitalRead(pin_down) && digitalRead(pin_select) ) {
        lcd.clear();
        OvenState = 1;
      }
      break;

    case 1:
      if (digitalRead(pin_up) ) {
        delay(100);
        minutes++;
      }
      if (digitalRead(pin_down) ) {
        delay(100);
        if (minutes )minutes--;
      }
      lcd.clear();
      lcd.print("Set timer:");
      lcd.setCursor(0, 1);
      lcd.print(minutes);
      if (digitalRead(pin_select) ) {
        minutes *= 60;
        OvenState = 2;
        delay(100);
      }
      break;
    case 2:
      while (minutes > 0 && OvenState == 2) {
        lcd.clear();
        lcd.print("Time remaining: ");
        lcd.setCursor(0, 1);
        lcd.print(sprintf("%i:%02i", minutes / 60, minutes % 60));
        delay(1000);
        minutes--;

        if (minutes == 0) {
          lcd.clear();
          lcd.print("Times Up!");
          delay(2000);
          OvenState = 3;
          break;
        }

        if (digitalRead(pin_select) )        {
          lcd.clear();
          minutes = 0;
          OvenState = 1;
          delay(200);
        }
      }
      break;
    case 3:
      seconds = 60;
      lcd.clear();
      lcd.print("Oven will shut off in: ");
      lcd.setCursor(0, 1);
      lcd.print("1:00");

      // Start pin_buzzer and flashing pin_light

      digitalWrite(pin_buzzer, LOW);
      digitalWrite(pin_light, LOW);
      unsigned long prevTime = millis();
      bool isButtonPressed = false;

      while (seconds > 0 && !isButtonPressed) {
        if (millis() - prevTime >= 500) {
          prevTime += 500;
          digitalWrite(pin_buzzer, !digitalRead(pin_buzzer));
          digitalWrite(pin_light, !digitalRead(pin_light));
          if (!digitalRead(pin_buzzer))seconds--;
        }
        lcd.setCursor(0, 1);
        
        lcd.print(sprintf("0:%02i", seconds));
        if (digitalRead(pin_select) == LOW)isButtonPressed = true;
      }

      // Turn off pin_buzzer and pin_light
      digitalWrite(pin_buzzer, LOW);
      digitalWrite(pin_light, LOW);

      lcd.clear();
      OvenState = 1;
  }
}

How are you sure for that

In addition to @anon57585045 response you must test your each and every hardware to ensure it usability and functionality before making final project

1 Like

What are your expectations of the forum?

Code without a schematic is useless. We can't tell what the code is trying to do the right thing without a schematic.

Hi, @fejiroo_o
Welcome to the forum.

Have you written your code in stages?
Each stage testing an individual input or output bit of hardware, to prove you have control and are getting the desired responses.

If you didn't, then STOP, and put your code aside for the moment, and write some code JUST for each peripheral and get the code working.

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

This is very important so we know how you have connected the hardware and what signals the hardware produces, or receives.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

This is the first mistake I spotted. You don't display "set timer" on the LCD.

In "case 1:" you are clearing and re-drawing the LCD every time through loop(). This is not a logic error but it will cause a lot of flicker.

In "case 2:" your 'minutes' variable is actually counting in seconds. Poor choice of names.

In "case 3:" you count down an additional 60 seconds AFTER the 'minute' (seconds) variable reaches 0.

Exactly Also after multiple request no circuit diagram is been shared

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.