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.