Pages: [1]   Go Down
Author Topic: LED coding  (Read 1230 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
        
  }
  }
}
 
« Last Edit: December 26, 2011, 04:50:38 pm by nbockelmann » Logged

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The line
Code:
;for blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5)
should be
Code:
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:
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.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks you!
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 627
Posts: 34233
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also noticed, you might want to put

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

  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:

  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:
void flash(int pin){
  digitalWrite(pin, HIGH);
  delay(1000);
  digitalWrite(pin, LOW);
  delay(1000);
}
Then call the function at the start of loop
Code:
flash(12);
flash(11);
flash(10);
Just some ideas.

Onions.
Logged

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

Pages: [1]   Go Up
Jump to: