Go Down

Topic: LED coding (Read 1 time) previous topic - next topic

nbockelmann

Dec 26, 2011, 10:24 pm Last Edit: Dec 26, 2011, 10:50 pm by nbockelmann Reason: 1
This may be in the wrong area but can somebody tell me whats wrong with the code? I have it all set up right, however, when I upload it it tells me to add extra characters. When I add them it tells me there are more wrong ones. Can some one fix the line that is giving my trouble? Im still new to this.)



Code: [Select]
void setup() {                
 // initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(13, OUTPUT);    
}

void loop() {
 digitalWrite(13, HIGH);   // set the LED on
 delay(1000);              // wait for a second
 digitalWrite(13, LOW);    // set the LED off
 delay(1000);              // wait for a second

             
 // initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(12, OUTPUT);


 digitalWrite(12, HIGH);   // set the LED on
 delay(1000);              // wait for a second
 digitalWrite(12, LOW);    // set the LED off
 delay(1000);  // wait for a second
 
 
// initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(11, OUTPUT);


 digitalWrite(11, HIGH);   // set the LED on
 delay(1000);              // wait for a second
 digitalWrite(11, LOW);    // set the LED off
 delay(1000);
 
 
// initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(10, OUTPUT);


 digitalWrite(10, HIGH);   // set the LED on
 delay(1000);              // wait for a second
 digitalWrite(10, LOW);    // set the LED off
 delay(1000);




// LED leads connected to PWM pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 8;
const int BLUE_LED_PIN = 7;

// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

// Length of time we spend showing each color
const int DISPLAY_TIME = 100; // In milliseconds





 // Cycle color from red through to green
 // (In this loop we move from 100% red, 0% green to 0% red, 100% green)
 for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
       redIntensity = 255-greenIntensity;
       analogWrite(GREEN_LED_PIN, greenIntensity);
       analogWrite(RED_LED_PIN, redIntensity);
       delay(DISPLAY_TIME);5
 

 // Cycle color from green through to blue
 // (In this loop we move from 100% green, 0% blue to 0% green, 100% blue)  
;for blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5)
       greenIntensity = 255-blueIntensity;
       analogWrite(BLUE_LED_PIN, blueIntensity);
       analogWrite(GREEN_LED_PIN, greenIntensity);
       delay(DISPLAY_TIME);5
 

 // Cycle cycle from blue through to red
 // (In this loop we move from 100% blue, 0% red to 0% blue, 100% red)    
 for (redIntensity = 0; redIntensity <= 255; redIntensity+=5)
       blueIntensity = 255-redIntensity;
       analogWrite(RED_LED_PIN, redIntensity);
       analogWrite(BLUE_LED_PIN, blueIntensity);
       delay(DISPLAY_TIME);5
       
 }
 }
}

 

Onions

The line
Code: [Select]
;for blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5)
should be
Code: [Select]
for (blueIntensity = 0; blueIntensity <= 255; blueIntensity +=5){

You added number 5s after some semi-colons for some reason, which will stop it working. Deleting the 5s and sorting out the for loop, we have:

Code: [Select]
void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second

               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(12, OUTPUT);


  digitalWrite(12, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(12, LOW);    // set the LED off
  delay(1000);  // wait for a second


// initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(11, OUTPUT);


  digitalWrite(11, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(11, LOW);    // set the LED off
  delay(1000);


// initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(10, OUTPUT);


  digitalWrite(10, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(10, LOW);    // set the LED off
  delay(1000);




// LED leads connected to PWM pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 8;
const int BLUE_LED_PIN = 7;

// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

// Length of time we spend showing each color
const int DISPLAY_TIME = 100; // In milliseconds





  // Cycle color from red through to green
  // (In this loop we move from 100% red, 0% green to 0% red, 100% green)
  for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
        redIntensity = 255-greenIntensity;
        analogWrite(GREEN_LED_PIN, greenIntensity);
        analogWrite(RED_LED_PIN, redIntensity);
        delay(DISPLAY_TIME);


  // Cycle color from green through to blue
  // (In this loop we move from 100% green, 0% blue to 0% green, 100% blue)
for (blueIntensity = 0; blueIntensity <= 255; blueIntensity +=5){
        greenIntensity = 255-blueIntensity;
        analogWrite(BLUE_LED_PIN, blueIntensity);
        analogWrite(GREEN_LED_PIN, greenIntensity);
        delay(DISPLAY_TIME);


  // Cycle cycle from blue through to red
  // (In this loop we move from 100% blue, 0% red to 0% blue, 100% red)   
  for (redIntensity = 0; redIntensity <= 255; redIntensity+=5)
        blueIntensity = 255-redIntensity;
        analogWrite(RED_LED_PIN, redIntensity);
        analogWrite(BLUE_LED_PIN, blueIntensity);
        delay(DISPLAY_TIME);
       
  }
  }
}


Which compiles fine.

Onions.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

nbockelmann


Grumpy_Mike

Quote
can somebody tell me whats wrong with the code?

You haven't posted it in a code box. Modify the original post:-
Select the code and hit the # icon, then click save.

Onions

Also noticed, you might want to put

Code: [Select]
digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second

  pinMode(12, OUTPUT);


  digitalWrite(12, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(12, LOW);    // set the LED off
  delay(1000);  // wait for a second

  pinMode(11, OUTPUT);


  digitalWrite(11, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(11, LOW);    // set the LED off
  delay(1000);

  pinMode(10, OUTPUT);


  digitalWrite(10, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(10, LOW);    // set the LED off
  delay(1000);

const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 8;
const int BLUE_LED_PIN = 7;

// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

// Length of time we spend showing each color
const int DISPLAY_TIME = 100; // In milliseconds


from loop into setup. If you want to flash each LED colour on before you start mixing colours, I'd put this into setup:
Code: [Select]

  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);

const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 8;
const int BLUE_LED_PIN = 7;

// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

// Length of time we spend showing each color
const int DISPLAY_TIME = 100; // In milliseconds


and this into the start of loop:

Code: [Select]

  digitalWrite(12, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(12, LOW);    // set the LED off
  delay(1000);  // wait for a second

  digitalWrite(11, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(11, LOW);    // set the LED off
  delay(1000);

  digitalWrite(10, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(10, LOW);    // set the LED off
  delay(1000);



Or you could turn it into a flash function:
Code: [Select]

void flash(int pin){
  digitalWrite(pin, HIGH);
  delay(1000);
  digitalWrite(pin, LOW);
  delay(1000);
}

Then call the function at the start of loop
Code: [Select]

flash(12);
flash(11);
flash(10);

Just some ideas.

Onions.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

Go Up