We made this code in class:
int red = 6;
int green = 7;
int blue = 8;
int pot = A0;
int potVal = 0;
int chosenColor = 0;
void setup() {
// put your setup code here, to run once:
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(pot, INPUT);
Serial.begin (9600);
}
void loop() {
// put your main code here, to run repeatedly:
potVal = analogRead(pot);
chosenColor = map(potVal, 0, 1023, 1, 3);
if(chosenColor = 1) {
redCycle();
}
else {
analogWrite(red,255);
}
if(chosenColor = 2) {
greenCycle();
}
else {
analogWrite(green,255);
}
if(chosenColor = 3) {
blueCycle();
}
else {
analogWrite(blue,255);
}
Serial.println(chosenColor);
Serial.println(potVal);
delay(500);
}
void redCycle() {
analogWrite(red,0);
for(int i=0; i < 256; i+=5) {
analogWrite(red, i);
}
}
void greenCycle() {
analogWrite(green,0);
for(int i=0; i < 256; i+=5) {
analogWrite(green, i);
}
}
void blueCycle() {
analogWrite(blue,0);
for(int i=0; i < 256; i+=5) {
analogWrite(blue, i);
}
}
When we open the serial monitor we find that chosenColor is always 3 regardless of what the potVal is reading. I am sure is something simple but we can't find where is the error. Please help.
analogWrite()doesn't work on pins 7 and 8, only on those bearing the "~" symbol.chosenColor = 3means “letchosenColortake the value3”.