Need help for my school project

hey, so i got this school project that i'm working on with my friends with so little arduino and code knowledge.

so, there's 5 lights and 3 push buttons

red led = indicates power
white led = indicates "ozone mode"
green led = indicates "uv mode"
yellow led = indicates "auto mode"
orange led = this is the ozone
blue led = this is the uv

button (naming from left to right)

button 1 = stop button
button 2 = auto button
button 3 = ozone/uv button

i'm trying to replicate a sterilizer fridge that our school have

button 3 :

so, the red led turns on forever indicating there are power. if i push the button 3 (ozone/uv) the orange led and the white led lights up for 60 minutes and after that they will turn off. if i push the button 3 again, instead of turning on the white and red, now the green led and the blue led lights up for 60 minutes and turn off after.
( im trying all my best to code this but it doesnt want to turn on the green and blue led after i pushed the button for the second time)

easier example :

button 3 : pushed 1 time

  • white led on
  • orange led on
    delay 60 minutes
  • white led off
  • orange led off

button 3 : pushed 1 more time

  • green led on
  • blue led on
    delay 60 minutes
  • green led off
  • blue led off

button 2(auto button)

if i push the button 2, the yellow led turned on indicating the auto mode. then the orange led turns on for 60 minutes indicating ozone, and then automatically the orange led turns off, without me pressing the button again, now the blue led turns on for another 60 minutes and turns off again after. then after the blue led off, the yellow led also turned off indicating the auto mode is off

easier example :

button 2 : pushed
yellow led on

orange led on
delay 60 minutes
orange led off

blue led on
delay 60 minutes
blue led off

yellow led off

button 1 (stop button)
so if the button 3 or 2 is on, if i push the button 1 or the stop button, all of the button 3 or 2 activity will turned off

and that pretty much it, i hope anyone can help me to code this and also teach me lol, thank you!

Capture

I have managed to code only the auto button but have no idea to code the other buttons

int st=1, au=2, ozo=3, ledp=4, ozone=5, ledozo=6, leduv=7, uv=8, ledau=9, o=0, a=0, s=0;
 int count = 0;

void setup (){
  
  pinMode (1, INPUT);
  pinMode (2, INPUT);
  pinMode (3, INPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);
  pinMode (9, OUTPUT);
 
    digitalWrite(4,1);
  	digitalWrite(5,0);
  	digitalWrite(6,0);
  	digitalWrite(7,0);
  	digitalWrite(8,0);
  	digitalWrite(9,0);
}

void loop () {
  digitalWrite(4,1);
  a = digitalRead(2);
  s = digitalRead(1);
  	if (s == HIGH)
    delay(100);
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
  	if (a == HIGH){
    delay(100);
    digitalWrite(5,1);
    digitalWrite(6,1);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
    delay(5000);
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
    
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,1);
    digitalWrite(8,1);
    digitalWrite(9,0);
    delay(5000);
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
	
  }
}

Make an attempt at coding it first. There are loads of examples out there and in your IDE.
After you've made an attempt and run into a specific issue, then come ask for help with that issue.

hey! i have managed to make the auto button working with this code, but i have no idea how to code the other buttons

int st=1, au=2, ozo=3, ledp=4, ozone=5, ledozo=6, leduv=7, uv=8, ledau=9, o=0, a=0, s=0;
 int count = 0;

void setup (){
  
  pinMode (1, INPUT);
  pinMode (2, INPUT);
  pinMode (3, INPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);
  pinMode (9, OUTPUT);
 
    digitalWrite(4,1);
  	digitalWrite(5,0);
  	digitalWrite(6,0);
  	digitalWrite(7,0);
  	digitalWrite(8,0);
  	digitalWrite(9,0);
}

void loop () {
  digitalWrite(4,1);
  a = digitalRead(2);
  s = digitalRead(1);
  	if (s == HIGH)
    delay(100);
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
  	if (a == HIGH){
    delay(100);
    digitalWrite(5,1);
    digitalWrite(6,1);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
    delay(5000);
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
    
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,1);
    digitalWrite(8,1);
    digitalWrite(9,0);
    delay(5000);
    digitalWrite(5,0);
    digitalWrite(6,0);
    digitalWrite(7,0);
    digitalWrite(8,0);
    digitalWrite(9,0);
	
  }
}

Use a code block for your code
Looks like this in the comment editor </>

Hi, @valentinorato
Welcome to the forum.

It will tell you how to post your code so it is in a scrolling window.

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

1 Like

Thanks Tom! greetings from indonesia!

You will need a 'state machine' and 'non blocking code'.
Search for those on this forum or via google.
Stefan made some nice posts on these subjects.
'Blink without delay' may get you started.
Also 'millis() timer' may provide some help.
Basically you cannot have delay(5000) in your code if you want your code to respond to inputs properly.

1 Like

Hi @valentinorato,

welcome to the arduino forum.
Your description of the wanted functionality is pretty good.

Whenever you want to go beyond a super-simple functionality of
switch LED on
wait 1 second
switch LED off
repeat

more lines of code are nescessary and more time to learn it.

The full functionality that you described will take you estimated 10 to 20 hours of time to learn and to write the code for it.

It will take dozens of specific questions to learn it.
You will get ALL these specific questions answered because posting code and asking specific questions about the code shows your own effort to learn it.

How easy to understand any kind of tutorial or examplecode is,
depends on

  • your knoweldge that you already have
  • how much explanation is given as comments or around the code

Understanding non-blocking timing will take some time
Understanding state-machines will take some time too.

I recommend that you

  • look into tutorials
  • use the code given in the tutorial and then
  • post this code and ask specific questions here in the forum.

To find out what your momentary knowledge-level about programming is
simply read this tutorial and at the

very first line that you don't understand

post your specific question

best regards Stefan

first, consider meaningful names for the various LEDS and buttons.

also, buttons are typically connected between the pin and ground, the pint configures as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

enum {
    LED_RD = 4,
    LED_WH = 5,
    LED_GN = 6,
    LED_YE = 7,
    LED_OR = 8,
    LED_BU = 9,
};

enum {
    LED_PWR  = LED_RD,
    LED_OZ_M = LED_WH,
    LED_UV_M = LED_GN,
    LED_AUTO = LED_YE,
    LED_OZ   = LED_OR,
    LED_UV   = LED_BU,
}

byte PinLeds [] = { LED_RD, LED_WH, LED_GN, LED_YE, LED_OR, LED_BU };
#define Nleds  sizeof(PinLeds)

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
enum {
    BUT_STP  = 1,
    BUT_AUTO = 2,
    BUT_OZUV = 3,
}

byte PinButs [] = { BUT_STP, BUT_AUTO, BUT_OZUV };
#define NButs  sizeof(PinButs)

// -----------------------------------------------------------------------------
void setup ()
{
    for (unsigned n = 0; n < Nbuts; n++)
        pinMode (PinButs [n], INPUT_PULLUP);

    for (unsigned n = 0; n < Nleds; n++)  {
        pinMode      (PinLeds [n], OUTPUT);
   	    digitalWrite (PinLeds [n], Off);
    }

   	digitalWrite (LED_PWR, On);  // pw
}

// -----------------------------------------------------------------------------
void ledsOff ()
{
    for (unsigned n = 1; n < Nleds; n++)    // skip 0, LED_PWR
   	    digitalWrite (PinLeds [n], Off);
}

// -----------------------------------------------------------------------------

void loop ()
{
    unsigned long
    if (LOW == digitalRead (BUT_STP))
        ledsOff ();

    // ...
}

given above, it shouldn't be hard to recognize a different button, turn on some LEDs and after a "delay" turn them off using ledOff(). the benefit of ledOff() is there's no need to keep track of which LEDs are on

but there are several issues with the above approach:

  • if the STOP button is intended to turn the LEDs off immediately, "delay()" can't be used. millis() can be used but also a flag indicating when the "timer" is active.
    if (tmrFlag && millis() - prevTIme >= period)  {
  • pressing a button needs to turn on the appropriate LEDs, set the period, capture the current time (prevTime = millis(); and set tmrFlag. the "timer" calls ledsOff() and clears tmrFlag

  • but you also have the case of recognizing subsequent cases of a button press and setting different LEDs (i.e. button-3). this suggests a button-press-count that is incremented each button press and determining which case is next

  • and then there's the button-2 sequence where after the timer expires, it needs to call ledsOff(), but then possibly set other LEDs on. this suggests a mode-count which the timer needs to possibly increment and turn on other LEDs as well as resetting the timer (capturing the latest timestamp and possibly resetting the period, OR recognizing that it's the last case in the sequence and simply call ledsOff() and disable the timer (i.e. tmrFlag = 0)

  • bear in mind that a mode-count of zero means just turn of the LEDs, but a non-zero value means to process a sequence. may make sense that mode-count is initially set to value >1 and is decremented after each sequence step so that when it is zero, the leds are turned of and tmr disabled as in other cases

please make an attempt

Capture
hey im trying to do multiple order in one arduino, im very new to arduino, can someone help me what is wrong with my code to have multiple ifs statemet? thanks

so im trying to replicate a sterilizer fridge our school have, the idea is when i push the 3rd button from the left (button 1 is left button 2 is middle button 3 is right)
the 5 and 6 led will turn on and off after 3 seconds
and then when i push the button one more time the 7 and 8 leds will turn on and off after 3 seconds aswell.
and when i push the button 2, the 5,6 led will turn on for 3 second and turn off, without me pushing the button again now the 7,8 leds turn on for 3 seconds and off again (so the button 2 is like an auto mode)

dont mind the 4 and 9 leds. the 4 is the power indicator and the 9 is when auto mode is triggered.

int st = 1, au = 2, ozo = 3, ledp = 4, ozone = 5, ledozo = 6, leduv = 7, uv = 8, ledau = 9, o = 0, a = 0, s = 0;
int count = 0;
int newcount;
void setup ()
{
  pinMode (1, INPUT);
  pinMode (2, INPUT);
  pinMode (3, INPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);
  pinMode (9, OUTPUT);
}

void loop ()
{
  digitalWrite(4, 1);
  if (digitalRead(3) == HIGH) {
    newcount = count + 1;
    {
      if (newcount != count)
      {
        switch (newcount)
        {
          case 1: digitalWrite(5, HIGH);
            digitalWrite(6, HIGH);
            delay(3000);
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            break;
          case 2: digitalWrite(6, LOW);
            digitalWrite(5, LOW);
            digitalWrite(7, HIGH);
            digitalWrite(8, HIGH);
            delay(3000);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
            digitalWrite(8, LOW);
            digitalWrite(9, LOW);
            break;

          default: digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
            digitalWrite(8, LOW);

            newcount = 0;
            break;

        }
        count = newcount;
      }

    }
    delay(500);



    if (digitalRead(2) == HIGH) {
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);

      delay(3000);

      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);

      delay(3000);

      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
    }
  }
}
  

 


            
              
                         
               
  

What is wrong?

What does the code actually do? How is that different from what you want?

Hello valentinorato

In general, you should not use magic numbers. The I/O pins love to have a functional name.

Have a nice day and enjoy coding in C++.

1 Like

First mistake is that there is extra bracket below newcount=count+1

Also if(newcount!=count) this will be always true

At last add delay (200); or a flag above newcount=count+1; so that it doesn't count multiple times when you press buttons

Your line out is a mess. Pres ctr-t and post again.
It is very important where you put { and }.
We, as humans, can better check those if the code is properly aligned.

a) don't use 0 or 1 as pin - use it for the serial monitor
b) add serial outputs after each if statement so you can follow your program logic.
c) press CTRL-T in the IDE and format your code - it makes it easier for you to read your code
d) finally, if you need our help, post your updated code, post what your code does (= the error), post what your code should do.

so im trying to replicate a sterilizer fridge our school have, the idea is when i push the 3rd button from the left (button 1 is left button 2 is middle button 3 is right)
the 5 and 6 led will turn on and off after 3 seconds
and then when i push the button one more time the 7 and 8 leds will turn on and off after 3 seconds aswell.
and when i push the button 2, the 5,6 led will turn on for 3 second and turn off, without me pushing the button again now the 7,8 leds turn on for 3 seconds and off again (so the button 2 is like an auto mode)

dont mind the 4 and 9 leds. the 4 is the power indicator and the 9 is when auto mode is triggered.

@valentinorato, please do not cross-post. Threads merged.

1 Like

Here is an example code to count button pressed and print in serial monitor

const int buttonPin = 2;   // input pin for push button
int buttonState = 0;       // variable to store the state of the push button
int buttonCount = 0;       // variable to store the number of button presses
bool buttonPressed = false; // flag to track if the button is currently pressed

void setup() {
Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the push button
  buttonState = digitalRead(buttonPin);

  // check if the button has just been pressed (rising edge)
  if (buttonState == HIGH && !buttonPressed) {
    // increment the button count
    buttonCount++;
    // print the button count to the serial monitor
    Serial.println(buttonCount);
    // set the button pressed flag
    buttonPressed = true;
  }
  // check if the button has just been released (falling edge)
  else if (buttonState == LOW && buttonPressed) {
    // clear the button pressed flag
    buttonPressed = false;
  }
}

I hope this will help you

1 Like

Dont expect anyone on this forum to do your school project for you.

The process you want has input states (from the buttons) and output states (to the leds) so is a good candidate for a state machine.
You can read about state machines with some simple examples here

the code you have is obscure. I have no way of knowing what you expect this to do

if you comment your code properly it will help you understand it.

whereas
digitalWrite(5, HIGH); //turn on green LED

ideally c code should be largely self-explanatory; such as
digitalWrite(GreenLed, ON);

Then "ON" should be defined by the following directive, which is an extra (probably unnecessary) job.

#define ON HIGH

An why not "GreenLed" as "greenLed" to adhere with camelCase convention?