Go Down

Topic: (Solved) My Code Works... Most Of The Time... (Read 515 times) previous topic - next topic

Jeremy1998

Oct 11, 2010, 05:25 am Last Edit: Oct 12, 2010, 12:19 am by Jeremy1998 Reason: 1
[size=12]Solved: The darn led was backwards... No idea how that happened...[/size]

In the following code, all of the Leds work fine... Except for the 3rd one. On the 3rd led, the first and third pot are switched. The first controlls red normally, and blue on the third led. The third pot controlls blue normally, and red on the third led.

Also, the green (middle) pot doesn't do anything on the 3rd led.  

Code: [Select]
const byte Red[4] = {13, 10, 7, 4};  
const byte Green[4] = {12, 9,6, 3};
const byte Blue[4] = {11, 8,5, 2};

int backButton = 14;
int forwardButton = 15;

int redPot = 0;
int greenPot = 1;
int bluePot = 2;

int backState;
int forwardState;

int previousBack;
int previousForward;

int redVal;
int greenVal;
int blueVal;

int LED;

void setup(){
for (int i = 0; i < sizeof(Red) / sizeof(Red[0]); ++i) {  
 pinMode (Red[i], OUTPUT);                              
}  

for (int i = 0; i < sizeof(Green) / sizeof(Green[0]); ++i) {  
 pinMode (Green[i], OUTPUT);                                
}

for (int i = 0; i < sizeof(Blue) / sizeof(Blue[0]); ++i) {  
 pinMode (Blue[i], OUTPUT);                                  
}

pinMode(backButton, INPUT);
pinMode(forwardButton, INPUT);

digitalWrite(backButton, HIGH);
digitalWrite(forwardButton, HIGH);
}

void loop(){
backState = digitalRead(backButton);
forwardState = digitalRead(forwardButton);

if(backState == LOW && previousBack == HIGH){
 LED = LED-1;
}  
else{
}

if(forwardState == LOW && previousForward == HIGH){
 LED = LED+1;
}  
else{
 
}  
if(LED == 5){
 LED = 1;
}  

if(backState == LOW && forwardState == LOW){
 for (int i = 0; i < sizeof(Red) / sizeof(Red[0]); ++i) {  
  digitalWrite(Red[i], LOW);    
 }
 
 for (int i = 0; i < sizeof(Green) / sizeof(Green[0]); ++i) {  
  digitalWrite(Green[i], LOW);
 }
 
 for (int i = 0; i < sizeof(Blue) / sizeof(Blue[0]); ++i) {
  digitalWrite(Blue[i], LOW);
 }
}  

redVal = analogRead(redPot);
greenVal = analogRead(greenPot);
blueVal = analogRead(bluePot);

map(redVal, 0, 1023, 0, 255);
map(greenVal, 0, 1023, 0, 255);
map(blueVal, 0, 1023, 0, 255);

analogWrite(Red[LED], redVal);
analogWrite(Green[LED], greenVal);
analogWrite(Blue[LED], blueVal);

previousBack = backState;
previousForward = forwardState;
}

Korman

#1
Oct 11, 2010, 06:35 am Last Edit: Oct 11, 2010, 06:36 am by Korman Reason: 1
Quote

Code: [Select]
int backButton = 14;
int forwardButton = 15;

int redPot = 0;
int greenPot = 1;


I hope you know, that the analog pin 0 = digital pin 14 etc. You have conflicts here.

Korman

Jeremy1998


PaulS

Code: [Select]
int LED;
It's a good idea to explicitly value all variables.

Code: [Select]
if(LED == 5){
 LED = 1;
}  

LED should be assigned the value of 0 when it gets to 5.

Code: [Select]
map(redVal, 0, 1023, 0, 255);
map(greenVal, 0, 1023, 0, 255);
map(blueVal, 0, 1023, 0, 255);

The map function does not alter the input value. It returns a value that you discard.

If you were doing this:
redVal = map(redVal, 0, 1023, 0, 255);
the result would, for this set of ranges, be the same as
redVal = redVal / 4;
(or redVal /= 4;)

Code: [Select]
analogWrite(Red[LED], redVal);
analogWrite(Green[LED], greenVal);
analogWrite(Blue[LED], blueVal);

Since LED can take on a value up to 4, which pins are affected by these calls, when LED IS 4?

Jeremy1998

Well, the error is on led 3... And when LED == 3, it is pins 7, 6, and 5.

PaulS

Quote
Well, the error is on led 3... And when LED == 3, it is pins 7, 6, and 5.

But, the value in LED can get to 4. The arrays only contain 4 values. The indices for those values are 0, 1, 2, and 3, not 1, 2, 3, and 4.

Go Up