serial moniotor stops reading

hi guys,

I'm working on a project which will eventually become an old phone with an "answering machine" which will basically be a finite state machine.
I'm taking a lot of inbetween steps before I make the final code because I'm terrible at coding.
so right now I have an LDR 5 buttons and 5 LEDs. the idea is that while the ldr is below a certain reading you can press the buttons to make the LEDs light up. while the LDR is above a certain reading pressing the buttons won't do anything.

this is the code I've made;

// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 11;
const int buttonPin4 = 10;
const int buttonPin5 = 9;// the number of the pushbutton pin
const int ledPin1 =  2; 
const int ledPin2 =  4;
const int ledPin3 =  5;
const int ledPin4 =  6;
const int ledPin5 =  7; // the number of the LED pin

const int ldrPin = A0; // this is the ldr under the horn of the phone

// variables will change:
int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
int buttonState5 = 0; // variable for reading the pushbutton status

void setup() {
 pinMode(ledPin1, OUTPUT);
 pinMode(buttonPin1, INPUT);
  pinMode(ledPin2, OUTPUT);
 pinMode(buttonPin2, INPUT);
  pinMode(ledPin3, OUTPUT);
 pinMode(buttonPin3, INPUT);
  pinMode(ledPin4, OUTPUT);
 pinMode(buttonPin4, INPUT);
  pinMode(ledPin5, OUTPUT);
 pinMode(buttonPin5, INPUT);

 
pinMode(ldrPin, INPUT);//ldr reads wheter or not the reciever is on

Serial.begin(9600);//we're gonna use the serial monitor later to check the ldr input

}

void loop() {
 // read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);

int ldrStatus = analogRead(ldrPin);

Serial.println(ldrStatus);

while (ldrStatus <=800) { // so the bottom stuff only happens while the receiver is off
 

 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
 if (buttonState1 == HIGH) {
   // turn LED on:
   digitalWrite(ledPin1, HIGH);
 } else {
   // turn LED off:
   digitalWrite(ledPin1, LOW);
 }

  if (buttonState2 == HIGH) {
   // turn LED on:
   digitalWrite(ledPin2, HIGH);
 } else {
   // turn LED off:
   digitalWrite(ledPin2, LOW);
 }

  if (buttonState3 == HIGH) {
   // turn LED on:
   digitalWrite(ledPin3, HIGH);
 } else {
   // turn LED off:
   digitalWrite(ledPin3, LOW);
 }

  if (buttonState4 == HIGH) {
   // turn LED on:
   digitalWrite(ledPin4, HIGH);
 } else {
   // turn LED off:
   digitalWrite(ledPin4, LOW);
 }

  if (buttonState5 == HIGH) {
   // turn LED on:
   digitalWrite(ledPin5, HIGH);
 } else {
   // turn LED off:
   digitalWrite(ledPin5, LOW);
 }
}
 
}

the LDR right now will give me a reading of above 800 but as soon as it has 1 or 2 below 800 it stops reading in the serial monitor. it also wont trigger any of the buttons to light up the LEDs.

You never change the value of ldrStatus in the while loop, so once that loop is entered, it can never be exited. You need to do the analogRead inside the while loop.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar, then you can manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Code tags please

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar, then you can manually add the code tags:

// your code is here

Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

thanks! I didn't know about this, very helpful explanation!

You never change the value of ldrStatus in the while loop, so once that loop is entered, it can never be exited. You need to do the analogRead inside the while loop.

okay so I've added another analogread inside the loop but I think I did it incorrectly because now all it's reading is 0's

const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 11;
const int buttonPin4 = 10;
const int buttonPin5 = 9;// the number of the pushbutton pin
const int ledPin1 =  2; 
const int ledPin2 =  4;
const int ledPin3 =  5;
const int ledPin4 =  6;
const int ledPin5 =  7; // the number of the LED pin

const int ldrPin = A0; // this is the ldr under the horn of the phone

// variables will change:
int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
int buttonState5 = 0; // variable for reading the pushbutton status

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin1, INPUT);
   pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin2, INPUT);
   pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin3, INPUT);
   pinMode(ledPin4, OUTPUT);
  pinMode(buttonPin4, INPUT);
   pinMode(ledPin5, OUTPUT);
  pinMode(buttonPin5, INPUT);

  
pinMode(ldrPin, INPUT);//ldr reads wheter or not the horn is on

Serial.begin(9600);//we're gonna use the serial monitor later to check the ldr input

}

void loop() {
  // read the state of the pushbutton value:
 buttonState1 = digitalRead(buttonPin1);
 buttonState2 = digitalRead(buttonPin2);
 buttonState3 = digitalRead(buttonPin3);
 buttonState4 = digitalRead(buttonPin4);
 buttonState5 = digitalRead(buttonPin5);

 int ldrStatus = analogRead(ldrPin);

 Serial.println(ldrStatus);

 while (ldrStatus <=800) { // so the bottom stuff only happens while the horn is off
   analogRead(ldrPin);
   Serial.println(ldrStatus);
  

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin1, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
  }

   if (buttonState2 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin2, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin2, LOW);
  }

   if (buttonState3 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin3, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin3, LOW);
  }

   if (buttonState4 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin4, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin4, LOW);
  }

   if (buttonState5 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin5, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin5, LOW);
  }
 }
  
}
 analogRead(ldrPin);

The analogRead function returns a value. You discard the value.

ldrStatus = analogRead(ldrPin);

The analogRead function returns a value. You discard the value.

so how would I fix that?

sorry I'm super new to coding stuff so I'm still a but fuzzy on how everything works

Put

ldrStatus = analogRead(ldrPin);  // put the value returned by analogRead in the ldrStatus variable

in place of

analogRead(ldrPin);  // read the ldr pin, but discard the value (not saved in any variable)
analogRead(ldrPin);  // read the ldr pin, but discard the value (not saved in any variable)

okay that worked so now I'm getting what seems to be a normal reading out of the ldr, however still none of the buttons trigger the LEDs. when the LDR is below 800

If you make changes to your code, please post the latest version.

How are the switches wired? Do you have pulldown resistors installed? A schematic would be most helpful.

You are not reading the buttonStates in the while loop either. Once the program enters the while the buttonStates are what they were in loop() and cannot change.

So you need:

  if (digitalRead(buttonState1) == HIGH) {  // button1 and so on

to read the buttons for the compare.

You are not reading the buttonStates in the while loop either. Once the program enters the while the buttonStates are what they were in loop() and cannot change.

So you need:
Code: [Select]

if (digitalRead(buttonState1) == HIGH) { // button1 and so on

to read the buttons for the compare.

I did this ;

const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 11;
const int buttonPin4 = 10;
const int buttonPin5 = 9;// the number of the pushbutton pin
const int ledPin1 =  2; 
const int ledPin2 =  4;
const int ledPin3 =  5;
const int ledPin4 =  6;
const int ledPin5 =  7; // the number of the LED pin

const int ldrPin = A0; // this is the ldr under the horn of the phone

// variables will change:
int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
int buttonState5 = 0; // variable for reading the pushbutton status

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin1, INPUT);
   pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin2, INPUT);
   pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin3, INPUT);
   pinMode(ledPin4, OUTPUT);
  pinMode(buttonPin4, INPUT);
   pinMode(ledPin5, OUTPUT);
  pinMode(buttonPin5, INPUT);

  
pinMode(ldrPin, INPUT);//ldr reads wheter or not the horn is on

Serial.begin(9600);//we're gonna use the serial monitor later to check the ldr input

}

void loop() {
  // read the state of the pushbutton value:
 buttonState1 = digitalRead(buttonPin1);
 buttonState2 = digitalRead(buttonPin2);
 buttonState3 = digitalRead(buttonPin3);
 buttonState4 = digitalRead(buttonPin4);
 buttonState5 = digitalRead(buttonPin5);

 int ldrStatus = analogRead(ldrPin);

 Serial.println(ldrStatus);

 while (ldrStatus <=800) { // so the bottom stuff only happens while the horn is off
   ldrStatus = analogRead(ldrPin); 
   Serial.println(ldrStatus);
  

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (digitalRead(buttonState1) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin1, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
  }

   if (digitalRead(buttonState2) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin2, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin2, LOW);
  }

   if (digitalRead(buttonState3) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin3, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin3, LOW);
  }

   if (digitalRead(buttonState4) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin4, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin4, LOW);
  }

   if (digitalRead(buttonState5) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin5, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin5, LOW);
  }
 }
  
}

however, now all my LED's just turn on as soon as the arduino is plugged in.

I've attached a schematic but I must warn you it may just give you a headache instead of explaining anything, its rather messy.
the buttons are 15 buttons in 5 groups of 3 buttons in parallel, each have a 10k resistor.