Arduino Menorah Not Working

Hi Everyone,

I am doing a school project where I have to build a menorah using 9 LEDs and a potentiometer to control the day. I have written the code, but when I run it, the serial monitor displays the potentiometer readout, but no LEDs are turning on. If anyone could help me, that would be amazing! I have attached the code, and a photo of the wiring.
Menorah Wiring.pdf (6.0 KB)

//declaring Variables
float readVal;
float finalVoltage;
int readPin = A0;
int one = 2;
int two = 3;
int three = 4;
int four = 5;
int chamish = 6;
int five = 7;
int six = 8;
int seven = 9;
int eight = 10;

void setup() {

//Starting the serial monitor
  Serial.begin(9600);
  
  pinMode(readPin, INPUT);
  pinMode(one, OUTPUT);
  pinMode(two, OUTPUT);
  pinMode(three, OUTPUT);
  pinMode(four, OUTPUT);
  pinMode(chamish, OUTPUT);
  pinMode(five, OUTPUT);
  pinMode(six, OUTPUT);
  pinMode(seven, OUTPUT);
  pinMode(eight, OUTPUT);
  
}

void loop() {

while (true) {
  readVal = analogRead(readPin);
  
 finalVoltage = (9*1023.)/readVal;


//priting the final voltage
  Serial.print("The Current Voltage is: ");
  Serial.println(finalVoltage);

}


 if ((finalVoltage >=270.) && (finalVoltage <= 230.)){
    goto day0;
    }

    if ((finalVoltage >=230.) && (finalVoltage <= 200.)){
 	goto day1;
  	} 
    
  if ((finalVoltage >=200.) && (finalVoltage <= 170.)){
      goto day2;
      } 
    
  if ((finalVoltage >=170.) && (finalVoltage <= 130.)){
        goto day3;
        } 
    
  if ((finalVoltage >=130.) && (finalVoltage <= 100.)){
          goto day4;
          } 
    
  if ((finalVoltage >=100.) && (finalVoltage <= 70.)){
            goto day5;
            } 
    
  if ((finalVoltage >=70.) && (finalVoltage <= 30.)){
              goto day6;
              } 
    
  if ((finalVoltage >=30.) && (finalVoltage <= 9.)){
                goto day7;
                } 
    
  if ((finalVoltage >=9.) && (finalVoltage <= 0.)){
                  goto day8;
                  }
    

  
 day0:

    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);


day1:
  
    digitalWrite(chamish, HIGH);
    delay(500);
  digitalWrite(one, HIGH);
    digitalWrite(two, LOW);
    digitalWrite(three, LOW);
    digitalWrite(four, LOW);
    digitalWrite(five, LOW);
    digitalWrite(six, LOW);
    digitalWrite(seven, LOW);
    digitalWrite(eight, LOW); 

day2:
digitalWrite(chamish, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
  digitalWrite(one, HIGH);
    digitalWrite(three, LOW);
    digitalWrite(four, LOW);
    digitalWrite(five, LOW);
    digitalWrite(six, LOW);
    digitalWrite(seven, LOW);
    digitalWrite(eight, LOW);
day3:
 digitalWrite(chamish, HIGH);
    delay(500);
    digitalWrite(three, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
  digitalWrite(one, HIGH);
    digitalWrite(four, LOW);
    digitalWrite(five, LOW);
    digitalWrite(six, LOW);
    digitalWrite(seven, LOW);
    digitalWrite(eight, LOW);
day4:
digitalWrite(chamish, HIGH);
    delay(500);
    digitalWrite(four, HIGH);
    delay(500);
    digitalWrite(three, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
  digitalWrite(one, HIGH);
    digitalWrite(five, LOW);
    digitalWrite(six, LOW);
    digitalWrite(seven, LOW);
    digitalWrite(eight, LOW);
day5:
digitalWrite(chamish, HIGH);
    delay(500);
    digitalWrite(five, HIGH);
    delay(500);
    digitalWrite(four, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
  digitalWrite(one, HIGH);
    digitalWrite(six, LOW);
    digitalWrite(seven, LOW);
    digitalWrite(eight, LOW);
day6:
digitalWrite(chamish, HIGH);
    delay(500);
    digitalWrite(six, HIGH);
    delay(500);
    digitalWrite(five, HIGH);
    delay(500);
    digitalWrite(four, HIGH);
    delay(500);
    digitalWrite(three, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
  digitalWrite(one, HIGH);
    digitalWrite(seven, LOW);
    digitalWrite(eight, LOW);
day7:
digitalWrite(chamish, HIGH);
    delay(500);
    digitalWrite(seven, HIGH);
    delay(500);
    digitalWrite(six, HIGH);
    delay(500);
    digitalWrite(five, HIGH);
    delay(500);
    digitalWrite(four, HIGH);
    delay(500);
    digitalWrite(three, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
  digitalWrite(one, HIGH);
    digitalWrite(eight, LOW);
day8:
digitalWrite(chamish, HIGH);
    delay(500);
    digitalWrite(eight, HIGH);
    delay(500);
    digitalWrite(seven, HIGH);
    delay(500);
    digitalWrite(six, HIGH);
    delay(500);
    digitalWrite(five, HIGH);
    delay(500);
    digitalWrite(four, HIGH);
    delay(500);
    digitalWrite(three, HIGH);
    delay(500);
    digitalWrite(two, HIGH);
    delay(500);
    digitalWrite(one, HIGH);
    
}

goto ?

Just say NO !

What else should I use? if statements?

Tell us what you think is happening here.

If a while( ) always evaluates, how do you get out of the loop ? :thinking:

Instead of using 'goto', create functions called day0, day1 etc and call them from inside your if statements.

Also use the code formatting tool in the IDE to sort out your indentation. It will make it easier to read.

I just want to get a readout from the potentiometer constantly, so it can figure out what LED's to turn on and off. If I remove the while() then all the LED's just turn on, and refuse to turn off. Also, then serial monitor then freezes after the first readout.

If a while( ) always evaluates, how do you get out of the while( ) loop ? :thinking:

i am not sure. I want it to take a readout, figure out which LEDs should turn on, turn the right ones on and off, then take a new readout. This should happen like every second.

One way would be to use if/else then turn the appropriate LEDs on/off as you do now

A better way would be to put the LED states for each day in an array and pass an index to the array row for the day selected using the pot to a function. This would require much less code as the function would be the same for every day, only the row of LED states to be turned on/off would be different

To turn the pot value into the array index consider using the map() function

I did that before, but all the LEDs just turned on, just like if I remove the while()

Post your code that did not work

The logic of your comparisons is wrong. None of them except the last one which turns on day8 can ever be true.

For day 1 you use this

if ((finalVoltage >=230.) && (finalVoltage <= 200.)){
 	goto day1;
  	}

What number for final voltage can be greater than 230 and less than 200?

You probably wanted the final voltage to be between 200 and 230 for day 1.

What is the symbol for greater than? What is the symbol for less than.

Shalom scooter_arduino613

Try, check and mod the sketch to you needs.

/* BLOCK COMMENT
  - https://forum.arduino.cc/t/arduino-menorah-not-working/1065953
  - Arduino Menorah Not Working 
  - This sketch may contain traces of C++.
  - In case of indisposition:
  - https://www.learncpp.com/
  - Hardware:
  - Thanks to LarryD
  - https://aws1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  - Tested @ Arduino: Mega[X] - UNO [ ] - Nano [ ]
  - https://forum.arduino.cc/u/paulpaulson/summary
*/
#define ProjectName "Arduino Menorah Not Working "
// make names
enum {Min, Max};
enum Days {day0, day1, day2, day3, day4, day5, day6, day7, day8};
// make variables
int readPin = A0;
int one = 2;
int two = 3;
int three = 4;
int four = 5;
int chamish = 6;
int five = 7;
int six = 8;
int seven = 9;
int eight = 10;


// make structures
struct MENORAH
{
  int dayName;
  const int frame[2];
  String day;
};
MENORAH menorahs[]
{
  {day0,{113 * 0, 113 * 1}, "day 0"},
  {day1,{113 * 1, 113 * 2}, "day 1"},
  {day2,{113 * 2, 113 * 3}, "day 2"},
  {day3,{113 * 3, 113 * 4}, "day 3"},
  {day4,{113 * 4, 113 * 5}, "day 4"},
  {day5,{113 * 5, 113 * 6}, "day 5"},
  {day6,{113 * 6, 113 * 7}, "day 6"},
  {day7,{113 * 7, 113 * 8}, "day 7"},
  {day8,{113 * 8, 113 * 9}, "day 8"},
};

void setup()
{
  Serial.begin(9600);
  Serial.print(ProjectName), Serial.print(" in "), Serial.println(__FILE__);
  pinMode(one, OUTPUT);
  pinMode(two, OUTPUT);
  pinMode(three, OUTPUT);
  pinMode(four, OUTPUT);
  pinMode(chamish, OUTPUT);
  pinMode(five, OUTPUT);
  pinMode(six, OUTPUT);
  pinMode(seven, OUTPUT);
  pinMode(eight, OUTPUT);
}
void loop()
{
  static int daySelected;
  int potRead = analogRead(readPin);
  for (auto &menorah : menorahs)
  {
    if ((potRead >= menorah.frame[Min]) && (potRead <= menorah.frame[Max]))
    {
      Serial.println (menorah.day);
      daySelected = menorah.dayName;
    }
  }
  switch (daySelected)
  {
    case day0:

      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      break;
    case day1:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      digitalWrite(two, LOW);
      digitalWrite(three, LOW);
      digitalWrite(four, LOW);
      digitalWrite(five, LOW);
      digitalWrite(six, LOW);
      digitalWrite(seven, LOW);
      digitalWrite(eight, LOW);
      break;
    case day2:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      digitalWrite(three, LOW);
      digitalWrite(four, LOW);
      digitalWrite(five, LOW);
      digitalWrite(six, LOW);
      digitalWrite(seven, LOW);
      digitalWrite(eight, LOW);
      break;
    case day3:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(three, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      digitalWrite(four, LOW);
      digitalWrite(five, LOW);
      digitalWrite(six, LOW);
      digitalWrite(seven, LOW);
      digitalWrite(eight, LOW);
      break;
    case day4:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(four, HIGH);
      delay(500);
      digitalWrite(three, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      digitalWrite(five, LOW);
      digitalWrite(six, LOW);
      digitalWrite(seven, LOW);
      digitalWrite(eight, LOW);
      break;
    case day5:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(five, HIGH);
      delay(500);
      digitalWrite(four, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      digitalWrite(six, LOW);
      digitalWrite(seven, LOW);
      digitalWrite(eight, LOW);
      break;
    case day6:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(six, HIGH);
      delay(500);
      digitalWrite(five, HIGH);
      delay(500);
      digitalWrite(four, HIGH);
      delay(500);
      digitalWrite(three, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      digitalWrite(seven, LOW);
      digitalWrite(eight, LOW);
      break;
    case day7:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(seven, HIGH);
      delay(500);
      digitalWrite(six, HIGH);
      delay(500);
      digitalWrite(five, HIGH);
      delay(500);
      digitalWrite(four, HIGH);
      delay(500);
      digitalWrite(three, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      digitalWrite(eight, LOW);
      break;
    case day8:
      digitalWrite(chamish, HIGH);
      delay(500);
      digitalWrite(eight, HIGH);
      delay(500);
      digitalWrite(seven, HIGH);
      delay(500);
      digitalWrite(six, HIGH);
      delay(500);
      digitalWrite(five, HIGH);
      delay(500);
      digitalWrite(four, HIGH);
      delay(500);
      digitalWrite(three, HIGH);
      delay(500);
      digitalWrite(two, HIGH);
      delay(500);
      digitalWrite(one, HIGH);
      break;
  }
}
1 Like

I figured you'd do it simply with 9 sequential if statements and incrementally illuminate an LED depending on the value of the pot.

loop(){
read potentiometer
if value > day1 then light Day 1 LED
if value > day2 then light Day 2 LED
...
}

where day1 value is something like 100 and day2 is something like 200...

I have tried that, but then all the LEDs would turn on, and refuse to turn off. Also, then serial monitor would stop. @paulpaulson has helped me re-do the code to make it functional.

Yeah, I saw that. If you’ve got it working, that’s all you need.

But I recommend you go back and take a look at what you were doing earlier, and figure out why it doesn’t work. There’s a lesson to be learned by analyzing that logic.

Enjoy!

You are absolutely correct! I have looked it over, and found some places where I went wrong.

This WOKWI project does essentially what the first post asks, IMO.

It's derived from IDE -> fIle/examples/display/bar graph

1 Like

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