Please help me with my code for my project

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.

const byte BUTTON_X_PIN = 8;
const byte BUTTON_Y_PIN = 9;
const byte SENSE_PIN = A1;
bool modeStateX = false;
bool modeStateY = false;

void setup() {
  DDRD |= B111100;//const int LED = {2, 3, 4, 5};
  pinMode(BUTTON_X_PIN, INPUT);
  pinMode(BUTTON_Y_PIN, INPUT_PULLUP);
}

void loop () {
  bool buttonX = digitalRead(BUTTON_X_PIN) == HIGH;
  bool buttonY = digitalRead(BUTTON_Y_PIN) == LOW;
  bool sensorVal = analogRead(SENSE_PIN) > 282;
  sensorVal?digitalWrite(13,HIGH)digitalWrite(13,LOW);

  if (buttonX && !buttonY) {
    modeStateX = true;
    modeStateY = false;
  }
  if (buttonY && !buttonX) {
    modeStateY = true;
    modeStateX = false;
  }
  if (sensorVal  ^ modeStateY)PORTD |= B111100;
  else PORTD &= ~B111100;
}

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:

StateChangeDetection

Hi Hector,

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

void setup()

void loop()

You should add more functions

measureLightIntensity()
ReadButtonsSetMode()
LEDsOn()
LEDsOff()
SwitchLEDs()

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

1 Like

Thank you for this. However, I would like to know what I am doing wrong so I can learn.

I want to switch modes by just pressing the buttons and not holding them.

Yeah, something is wrong with my logic. I'll try this to figure it out.

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

1 Like

If no button is pressed the (4) led lights are all off.

look into your code.

The above text says what you have to change.
make an attempt to modify your code.
Or at least ask a specific

question

not only a statement what is already obvoius

Be the change you want to see in the world
best regards Stefan

const byte SIZE = 4;
const byte LED[SIZE] = {2, 3, 4, 5};
const byte BUTTON_X_PIN = 8;
const byte BUTTON_Y_PIN = 9;
const byte SENSE_PIN = A1;

// Button states
bool buttonX = false;
bool buttonY = false;

// Led states
bool modeStateX = false;
bool modeStateY = false;

// Photoresistor value
int sensorVal = 0;;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < SIZE; i++) {
    pinMode(LED[i], 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);
  sensorVal = map(sensorVal, 110, 330, 0, 255);

  // ModeX
  if ((buttonX == true || modeStateX ) && buttonY == true) {
    modeStateX = true;
    modeStateY = false;
    if (sensorVal > 200) LedOn();
    else LedOff();
  }
  
  // ModeY
  else if ((buttonY == false || modeStateY ) && buttonX == false) {
    modeStateY = true;
    modeStateX = false;
    if (sensorVal > 200) LedOff();
    else LedOn();
  }
}

void LedOff() {
  for (byte i = 0; i < SIZE; i++)
    digitalWrite(LED[i], LOW);
}

void LedOn() {
  for (byte i = 0; i < SIZE; i++)
    digitalWrite(LED[i], HIGH);
}
1 Like

I was able to figure it out with your previous post. I missed that big time. Thank you for your help.

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