0

I'm working on TinkerCAD, I want to design and simulate a circuit in which there are following components. 1. 1 RGB LED 2. 1 Push button 3. 3 Photo-resistors. 4. 1 Arduino board. So the light should be off at the starting. When the push button is pressed for the first ever time, the LED should light up as red, and then the brightness of the red color should be able to controlled by one of the three photo-resistors used, the second time when the push-button is pressed the LED should light up as green and green color this time should be controlled by another photo-resistor..when the button is pressed for the third time , the LED should now change to blue color and the third photo-resistor should be able to control the brightness of the blue color. And this cycle should go on, upon pressing the Push button.

please tell me what's wrong.

Code looks like this

int button_state = 0;//declaring a variable
int counter;//declaring another variable,will keep count of which color to be lighted 
const int pRed = A2; //assigning pin A2 as pRed
const int pBlue = A1; //.............A1....pBlue
const int pGreen = A0; //............A0.....pGreen
int inputRed;//adjusted value of led brightness will be stored
int inputGreen;
int inputBlue;
const int ledRed = 9;
const int ledGreen = 11;
const int ledBlue = 10;

void setup()
{
  pinMode(2,INPUT);
  pinMode(ledRed,OUTPUT);
  pinMode(ledGreen,OUTPUT);
  pinMode(ledBlue,OUTPUT);
  pinMode(pRed,INPUT);
  pinMode(pGreen,INPUT);
  pinMode(pBlue,INPUT);
}

void loop()
{
  button_state = digitalRead(2);// reading the button state 
  inputRed = analogRead(pRed);// Reading the value from A2 pin 
  inputRed = map(inputRed,0,1023,0,255);//value from A2 pin adjusted for led's brightness
  if (button_state == HIGH) {
    counter = 0; // Declaring counter as 0 again
    analogWrite(ledRed,inputRed);
    analogWrite(ledGreen, 0);
    analogWrite(ledBlue, 0);
    delay(10);// Wait for 10 millisecond(s)
    green();// Jumping to green
  }
}
void green()
{
  while (counter == 0){//counter was declared 0 in loop function,so the code won't go ahead till it's 0 only
  button_state = digitalRead(2);//for reading the button state once again
  inputGreen = analogRead(pGreen);//reading the signal from A0 as it is the inputpin for photoresistor for green light
  inputGreen = map(inputGreen,0,1023,0,255);  //value from A0 pin mapped
  if (button_state == HIGH) {
    counter++; // Increasing counter by 1 so that it becomes 1 now.
    analogWrite(ledRed,0); 
    analogWrite(ledGreen,inputGreen);
    analogWrite(ledBlue,0);
    delay(10);//wait for 10 millisecond(s)
    blue(); //jumping to function for blue led now
  }
}
}
void blue()
{
  while (counter == 1)//using while, so that the code won't go ahead if counter=1,as made in function for green light
  {
  button_state = digitalRead(2);
  inputBlue = analogRead(pBlue);//reading value from A1 pin
  inputBlue = map(inputBlue,0,1023,0,255);//value from A1 pin mapped
  if (button_state == HIGH) {//
    counter++;
    analogWrite(ledRed,0);
    analogWrite(ledGreen,0);
    analogWrite(ledBlue,inputBlue);
    delay(10);//wait for 10 millisecond(s)
  }
  }
}


1

please tell me what's wrong.

I'm sorry to say, but pretty much everything. You need to rethink your program flow from the ground up.

Separate out the button from the LED actions. The button should only control the counter. The counter then makes the decision what to do with the LEDs.

Also you have to learn about the concept of change with the button. You don't want to be doing something if the button is high, since that will be being done all the time the button is pressed. Instead you need to learn how to do something only at the moment the button changes from low to high.

As a rough outline to how your program could look, here's a pseudo-listing:

  • Set the counter to 0
  • If the button changes from LOW to HIGH
    • Increment the counter
  • If the counter is greater than 3
    • Set the counter to 1

That would give you a sequence of 0-1-2-3-1-2-3-1-2-3 for the counter as you press the button.

Next you have a separate section in your loop to deal with what the counter is.

  • If the counter is 0
    • Turn off all LEDs
  • If the counter is 1
    • Control the red LED and turn off the others
  • If the counter is 2
    • Control the green LED and turn off the others
  • If the counter is 3
    • Control the blue LED and turn off the others

If you implement that properly you will have created a finite state machine.

| improve this answer | |
  • Okay can you give me a rough outline of the commands that can be used when the button is changed from LOW to HIGH, I've really tried everything I know. An example of something similar would also suffice. – Mahima Pareek Jun 4 at 11:54
  • 1
    digitalRead(), if() and remembering what the button state was. – Majenko Jun 4 at 12:27
  • Plus you'll need to debounce the button presses. (When you detect the button change from low to high, record the value of millis, and ignore further changes for 20-100 mS.) – Duncan C Jun 4 at 20:16
  • @DuncanC Depends how many delays you end up littering around the place... But yes, decent debouncing is always good. – Majenko Jun 4 at 20:19
  • 1
    @DuncanC I know, but it's not something you want to do every time. That's why I wrote my own library to do it. Write once, use many. Why write the same code every time? – Majenko Jun 4 at 20:42
0

Your code might look something like this

#define debounceTime 50   //Tweak this value to adjust amount of debouncing
unsigned long lastButtonMillis = 0;
uInt8 switchState = 0;
uInt8 oldButtonState = LOW;

void setup() {
   //Your setup code
}

void handleSwitchChange(uInt8 switchState) {
   switch switchState {
      case 0:
         //code for red
         break;

      case 1:
         //code for green
         break;

      case 1:
         //code for blue
         break;

   }
}

void loop() {
    //If enough time has elapsed, check for button presses
    if (millis() - lastButtonMillis >= debounceTime) {
        lastButtonMillis = millis();
        if (oldButtonState == LOW && digitalRead(switchPin) == HIGH) {
            oldButtonState = HIGH;
            switchState = (switchState + 1) % 3; //value will cycle 0,1,2,0,1,2
            handleSwitchChange(switchState);
        }
    } else { 
        oldButtonState = digitalRead(switchPin);
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.