Hi all, I need help with a project I am doing which consists of (2) switches, (1) Photoresisitor, and (4) led's.
What I want my project to do:
Mode X: I press button X(Pull-down) and the (4) led's light up. Then when I cover the photoresistor the (4) led's turn off and then turn back on as I uncover the photoresistor.
Mode Y: I press button Y(Pull-up) and the (4) led's are not lit. Then when I cover the photoresistor the (4) led's turn on and then turn off as I uncover the photoresistor. This is the opposite of mode X.
I have tested out the modes individually and both work correctly. However, I am having trouble combing them and having them both work (mainly going from mode X to Y). When I do combine them, I can go from mode Y to X no problem. But when I go back to mode Y the led's just blink once and stay in mode X. Going from mode X to Y only works if I hold down button Y. I want to be able to just switch modes by pressing the buttons.
// Connection pins
const int SIZE = 4;
const int LED[SIZE] = {2, 3, 4, 5};
const int BUTTON_X_PIN = 8;
const int BUTTON_Y_PIN = 9;
const int SENSE_PIN = A1;
// Button states
bool buttonX;
bool buttonY;
// Led states
bool modeStateX = LOW;
bool modeStateY = LOW;
// Photoresistor value
int sensorVal;
void setup() {
Serial.begin(9600);
// Led output
for (int i = 0; i < SIZE; i++) {
pinMode(LED[i], OUTPUT);
}
// Button output
pinMode(BUTTON_X_PIN, INPUT);
pinMode(BUTTON_Y_PIN, INPUT_PULLUP);
}
void loop () {
buttonX = digitalRead(BUTTON_X_PIN);
buttonY = digitalRead(BUTTON_Y_PIN);
sensorVal = analogRead(SENSE_PIN);
Serial.println(sensorVal);
//delay(500);
sensorVal = map(sensorVal, 110, 330, 0, 255);
// ModeX
if ((buttonX == HIGH || modeStateX == HIGH) && buttonY == HIGH) {
modeStateX = HIGH;
if (sensorVal > 200) {
for (int i = 0; i < SIZE; i++) {
digitalWrite(LED[i], HIGH);
}
}
else {
for (int i = 0; i < SIZE; i++) {
digitalWrite(LED[i], LOW);
}
}
}
// ModeY
else if ((buttonY == LOW || modeStateY == HIGH) && buttonX == LOW) {
modeStateY = HIGH;
if (sensorVal > 200) {
for (int i = 0; i < SIZE; i++) {
digitalWrite(LED[i], LOW);
}
}
else {
for (int i = 0; i < SIZE; i++) {
digitalWrite(LED[i], HIGH);
}
}
}
}
I see you are using the serial. Print() to show some values.
Add code to serial.Print() each of the variables in the mode tests prior to the "if" for each mode. Then you can follow the logic and see which one has a problem.
I assume by this you mean that you want to switch modes by momentarily holding them down. In that case I would suggest you study the state detection tutorial. What you are doing in your code you are checking the STATE of the button. What you really want to check is the transition from the button not being pressed to being pressed. Study the following tutorial:
welcome to the arduino-forum.
Well done posting your code as a code-section.
Your "project" sounds like a microcontroller/electronics exercise.
I will write about programming-tecniques.
C++ offers the use of functions. With functions you can organise your code into parts where each part is a senseful unit doing one thing. For example switching LEDs on/off
The arduino-IDE forces each user to use at least two functions
these names are userdefinable you could them
blabla()
myFunction2()
iuzwiuekd123()
but these names would confuse instead of beeing self-explaining what the function does.
To set an operation-mode through a short button-press and then release the button again
requires to set a mode-variable to "mode X" or to "mode Y"
In your case it will be sufficient to use a simple if-condition for each button to set the mode
The other operations must be made conditional to the mode
I'm pretty sure that you have new questions after reading this.
I can't read your mind what your questions are. So just post these questions.
Be the change you want to see in the world
best regards Stefan
ok. look on your code. what will going if no button is pressed? nothing, before some button is pressed
and where modeStateX and modeStateY going back to false? nowhere