80Views5Replies

Author Options:

Help with this Arduino sketch please... Answered

I got this sketch for flashing LED's off a site the other day and have been playing with it to try and have a couple of the LED's on all the time but I have failed, can someone please give me some help.

int sensorValue = 0;    //make a variable where you can store incoming
                        //analog values

void setup(){
  pinMode(12, OUTPUT);  //tell arduino what you'll be using these pins
  pinMode(11, OUTPUT);  // for (output).
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);

  Serial.begin(9600);  //initialize serial
}

void loop(){      //we put the code we want executed in a loop

Serial.print("sensor = " );  //sends what's in quotes via serial
Serial.println(sensorValue); //sends our variable (sensorValue)
                             //via serial  

  digitalWrite(12,HIGH);       // lights the led
  sensorValue = analogRead(0); // reads pin 0
  delay(sensorValue + 25);     // sensorValue used for delay
  digitalWrite(12,LOW);        //turns off the led
  delay(15);                   //delay before moving to next output pin
                               //the + 25 keeps delay from reaching zero
           //code below is for remaining 4 LEDs
  digitalWrite(11,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(11,LOW);
delay(15);

  digitalWrite(10,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(10,LOW);
delay(15);

  digitalWrite(9,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(9,LOW);
delay(15);

  digitalWrite(8, HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(8, LOW);
delay(15);

  digitalWrite(9,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(9,LOW);
delay(15);

  digitalWrite(10,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(10,LOW);
delay(15);

  digitalWrite(11,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(11,LOW);
delay(15);

}

Discussions

0
None
steveastrouk

Best Answer 4 years ago

Try this: the changes are at the top of the loop() section.

void loop(){ //we put the code we want executed in a loop

Serial.print("sensor = " ); //sends what's in quotes via serial
Serial.println(sensorValue); //sends our variable (sensorValue)
//via serial

digitalWrite(12,HIGH); // lights the led
digitalWrite(11,HIGH);

digitalWrite(10,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(10,LOW);
delay(15);

digitalWrite(9,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(9,LOW);
delay(15);

digitalWrite(8, HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(8, LOW);
delay(15);

digitalWrite(9,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(9,LOW);
delay(15);

digitalWrite(10,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(10,LOW);
delay(15);

digitalWrite(11,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(11,LOW);
delay(15);

}

0
None
steveastrouk

4 years ago

How have you failed ? What does your code not do ? How are your LEDs connected ?

0
None
lifeofBsteveastrouk

Answer 4 years ago

I have no idea how to stop some of the LED's blinking, I have tried changing some of the lines and values but with no luck. This is my first ever project so I am a bit lost. I have managed to add a few more LED's, another pot and an on/off switch to my breadboard, and the next step is too figure out how to make only some LED's flash and some remain on all the time.

P1070813.JPG