In: Computer Science
CODE WITH ARDUINO: With an RGB Led, Use the iterative loop (for loop) to make red, blue, and green. Use ‘analogWrite’ command inside the 'for loop.' Use the value ranges from 0-255 for this command. So, to activate any of the pins, the value should be 255. To turn off 0.
pattern:
First for loop Red light – delay – Second for loop for blue light – delay - Third for loop for green light - delay.
(The resulting lights will be all the combinations of three colors and of course red, blue and green)
Here our task is to create an arduino program which produce RED BLUE GREEN colour from an rgbled using 3 for loops
Points to remember before coding
CODE
Working
(screenshots taken while simulating above code are shown below)
Text version of the code is given below
int red=A0; //declaring variables at which led doing to be
connected
int green=A1;
int blue=A2;
int i,j,k; /iterating variables for for-loops
void setup() {
pinMode(red,OUTPUT); //setting led pins as output
pinMode(green,OUTPUT);
pinMode(red,OUTPUT);
}
void loop() {
for(i=0;i<50;i++){ // for loop for red
analogWrite(red,255); //turning red on
}
analogWrite(red,0); //turning red off after loop
delay(200); //delay
for(i=0;i<50;i++){
analogWrite(blue,255);
}
analogWrite(blue,0);
delay(200);
for(i=0;i<50;i++){
analogWrite(green,255);
}
analogWrite(green,0);
delay(200);
}
Note :But remember the above task can be easily implemented without a for loop as follow