Pages: [1]   Go Down
Author Topic: (Solved) My Code Works... Most Of The Time...  (Read 423 times)
0 Members and 1 Guest are viewing this topic.
Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[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:
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;
}
« Last Edit: October 11, 2010, 05:19:32 pm by Jeremy1998 » Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 6
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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
« Last Edit: October 10, 2010, 11:36:10 pm by Korman » Logged

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino Mega
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
if(LED == 5){
  LED = 1;
 }  
LED should be assigned the value of 0 when it gets to 5.

Code:
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:
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?
Logged

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: