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)
}
}
}