Interactive LED "I NEED HELP"

Ok guys i need a little help. i'm still new to the arduino and programming altogether. I felt like I was at the point that i could write my first Program, Im calling it " Interactive LED ". the plan is to have a line of 10 LEDs with Two buttons at start up of the arduino only one led should be on and as i press on button the light should move to the next LED( move down the line X amount of button presses) and if i press the other button the light should move in the opposite direction ( move up the line X amount of Button presses).

so i wrote the program to what i thought was right(i was wrong) it will not run correctly. when i power up the arduino all but one LED lights up and the buttons do nothing.

here is the program, tell me what i did wrong (also a schematic)

#define led1 9
#define led2 8
#define led3 7
#define led4 6
#define led5 5
#define led6 4
#define led7 3
#define led8 2
#define led9 1
#define led10 0
#define sensorA 12
#define sensorB 13
int val = (0);
int state = 0;

void setup(){
pinMode(led1 , OUTPUT);
pinMode(led2 , OUTPUT);
pinMode(led3 , OUTPUT);
pinMode(led4 , OUTPUT);
pinMode(led5 , OUTPUT);
pinMode(led6 , OUTPUT);
pinMode(led7 , OUTPUT);
pinMode(led8 , OUTPUT);
pinMode(led9 , OUTPUT);
pinMode(led10 , OUTPUT);
pinMode(sensorA , INPUT);
pinMode(sensorB , INPUT);
}

void loop(){
val = digitalRead(sensorA);
if (val = HIGH) {
state + 1;}
val = digitalRead(sensorB);
if (val = HIGH) {
state - 1;
}
if (state = 0) {
digitalWrite(led1, HIGH);
}
if (state = 1) {
digitalWrite(led2, HIGH);
}
if (state = 2) {
digitalWrite(led3, HIGH);
}
if (state = 3) {
digitalWrite(led4, HIGH);
}
if (state = 4) {
digitalWrite(led5, HIGH);
}
if (state = 5) {
digitalWrite(led6, HIGH);
}
if (state = 6) {
digitalWrite(led7, HIGH);
}
if (state = 7) {
digitalWrite(led8, HIGH);
}
if (state = 8) {
digitalWrite(led9, HIGH);
}
if (state = 9) {
digitalWrite(led10, HIGH);
}
}

Interactive LED-Model.pdf (16.4 KB)

Interactive LED .ino (1.12 KB)

if (val = HIGH) {

This sets 'val' to HIGH. You want:

 if (val == HIGH) {
  state + 1;

This adds two values together and throws away the result. You want:

  state = state + 1;}
or
  state += 1;
or
  state++;

Similarly for "state - 1" You want:

  state = state - 1;}
or
  state -= 1;
or
  state--;

Fix those mistakes and all the similar mistakes in your code.

Then, let us know when/if LEDs won't turn off again.

with the update all LEDs are off but one(on start up)
when i press button to pin 12 all LEDs light up
if button on pin 13 is pressed nothing changes(unless i press it 7 times, then the same light configuration appears

updated code:

#define led1 9
#define led2 8
#define led3 7
#define led4 6
#define led5 5
#define led6 4
#define led7 3
#define led8 2
#define led9 1
#define led10 0
#define sensorA 12
#define sensorB 13
int val = (0);
int state = 0;

void setup(){
pinMode(led1 , OUTPUT);
pinMode(led2 , OUTPUT);
pinMode(led3 , OUTPUT);
pinMode(led4 , OUTPUT);
pinMode(led5 , OUTPUT);
pinMode(led6 , OUTPUT);
pinMode(led7 , OUTPUT);
pinMode(led8 , OUTPUT);
pinMode(led9 , OUTPUT);
pinMode(led10 , OUTPUT);
pinMode(sensorA , INPUT);
pinMode(sensorB , INPUT);
}

void loop(){
val = digitalRead(sensorA);
if (val == HIGH) {
state = state + 1;}
val = digitalRead(sensorB);
if (val == HIGH) {
state = state - 1;
}
if (state == 0) {
digitalWrite(led1, HIGH);
}
if (state ==1) {
digitalWrite(led2, HIGH);
}
if (state == 2) {
digitalWrite(led3, HIGH);
}
if (state == 3) {
digitalWrite(led4, HIGH);
}
if (state == 4) {
digitalWrite(led5, HIGH);
}
if (state == 5) {
digitalWrite(led6, HIGH);
}
if (state == 6) {
digitalWrite(led7, HIGH);
}
if (state == 7) {
digitalWrite(led8, HIGH);
}
if (state == 8) {
digitalWrite(led9, HIGH);
}
if (state == 9) {
digitalWrite(led10, HIGH);
}
}

The_New_Guy:
with the update all LEDs are off but one(on start up)
when i press button to pin 12 all LEDs light up
if button on pin 13 is pressed nothing changes(unless i press it 7 times, then the same light configuration appears

Use [ code][ /code] tags.

updated code:

#define led1 9

#define led2 8
#define led3 7
#define led4 6
#define led5 5
#define led6 4
#define led7 3
#define led8 2
#define led9 1
#define led10 0

Avoid the use of pins 0 and 1 as they are used by serial input and output.

#define sensorA 12
#define sensorB 13
int val = (0);

Why the brackets around the 0?

int state = 0;

void setup(){
pinMode(led1 , OUTPUT);
pinMode(led2 , OUTPUT);
pinMode(led3 , OUTPUT);
pinMode(led4 , OUTPUT);
pinMode(led5 , OUTPUT);
pinMode(led6 , OUTPUT);
pinMode(led7 , OUTPUT);
pinMode(led8 , OUTPUT);
pinMode(led9 , OUTPUT);
pinMode(led10 , OUTPUT);
pinMode(sensorA , INPUT);
pinMode(sensorB , INPUT);
}

void loop(){
val = digitalRead(sensorA);
if (val == HIGH) {
  state = state + 1;}

Here is your problem. This will increment state every time round the loop, which happens every few MICROseconds. I doubt that you can release the button that quickly. Add a delay here.

delay(1000);  //delay 1 second
val = digitalRead(sensorB);
if (val == HIGH) {
  state = state - 1;
}

Same here.

Also have you considerd what happens when state goes negitave or over the value of 9?

yea last night after trying this "update" i relized that the LEDs were not coming on at the same time that there was a small delay between the first and last. so i add the delay(200) i found that 1 second was to long. well this fixed half of the problem, the light would move up but not down, and as it moved up the LED behind it would stay on. anyway i got it working, and ill just show u the code for you to see how i fixed it

code:

#define led1 9
#define led2 8
#define led3 7
#define led4 6
#define led5 5
#define led6 4
#define led7 3
#define led8 2
#define led9 1
#define led10 0
#define sensorA 12
#define sensorB 13
int val = 0;
int state = 0;

void setup(){
pinMode(led1 , OUTPUT);
pinMode(led2 , OUTPUT);
pinMode(led3 , OUTPUT);
pinMode(led4 , OUTPUT);
pinMode(led5 , OUTPUT);
pinMode(led6 , OUTPUT);
pinMode(led7 , OUTPUT);
pinMode(led8 , OUTPUT);
pinMode(led9 , OUTPUT);
pinMode(led10 , OUTPUT);
pinMode(sensorA , INPUT);
pinMode(sensorB , INPUT);
}

void loop(){
val = digitalRead(sensorB);
if (val == HIGH) {
state = state - 1;
}
delay(200);
val = digitalRead(sensorA);
if (val == HIGH) {
state = state + 1;
}
delay(200);
if (state == 0) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state ==1) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state == 2) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state == 3) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state == 4) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state == 5) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state == 6) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state == 7) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, HIGH);
digitalWrite(led9, LOW);
digitalWrite(led10, LOW);
}
if (state == 8) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, HIGH);
digitalWrite(led10, LOW);
}
if (state == 9) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
digitalWrite(led9, LOW);
digitalWrite(led10, HIGH);
}
}

Grumpy_Mike:
Also have you considerd what happens when state goes negitave or over the value of 9?

yes i have, i dont have a fix for it in this program because it was just a self test of what i have learned to see if i could write my own and not screw it up(i failed). but i will refine this program and fix that when i get to the end of the line it just goes to the other end. Also i will try to replace the buttons with a analog input and see if i can move the light this way

Thanks for the help Guys

The_New_Guy:
with the update all LEDs are off but one(on start up)
when i press button to pin 12 all LEDs light up
if button on pin 13 is pressed nothing changes(unless i press it 7 times, then the same light configuration appears

Hi New Guy,

What you haven't got used to the idea of yet is just how fast your program is executing on the Arduino. Even during the briefest button press you can manage with your finger, the "state" will have gone up (or down) by at least several hundred, maybe several thousand. That's why all 12 leds light up.

As Mike says, there's no code in your sketch to stop your "state" going above 11 or below zero.

When you press the pin 13 button 7 times, "state" goes so negative that it goes below -32768 and comes back around to +32767 and eventually down to 11, 10, 9 etc and all your leds are on again.

All 12 leds are on because nothing in the sketch ever switches your leds off again!

Paul

PaulRB:

The_New_Guy:
with the update all LEDs are off but one(on start up)
when i press button to pin 12 all LEDs light up
if button on pin 13 is pressed nothing changes(unless i press it 7 times, then the same light configuration appears

Hi New Guy,

What you haven't got used to the idea of yet is just how fast your program is executing on the Arduino. Even during the briefest button press you can manage with your finger, the "state" will have gone up (or down) by at least several hundred, maybe several thousand. That's why all 12 leds light up.

As Mike says, there's no code in your sketch to stop your "state" going above 11 or below zero.

When you press the pin 13 button 7 times, "state" goes so negative that it goes below -32768 and comes back around to +32767 and eventually down to 11, 10, 9 etc and all your leds are on again.

All 12 leds are on because nothing in the sketch ever switches your leds off again!

Paul

yea I'm still REALLY new to the arduino or programming altogether, but i really want to learn. Its a pretty easy concept to understand, I have been working with and learning about the arduino and programming since 8-16-13. So for now I'm just playing around with it, until i fell confident with the device, at that point i will start the project that i bought this thing for, and that's to build my own robot

Now that you've got something working, I recommend that you look for ways to make your sketch more robust, shorter and simpler.

You need to decide what you want to happen when you reach the last LED - do you want it to ignore further button presses, or start again from the far end?

The large chunk of code setting the LEDs on or off in the different states could be reduced to a few lines of code using an array to hold the LED pin numbers and a for loop to turn each LED on or off.

The Debounce example sketch shows how to read switch inputs that are vulnerable to contact bounce.

The_New_Guy:
yea last night after trying this "update" i relized that the LEDs were not coming on at the same time that there was a small delay between the first and last. so i add the delay(200) i found that 1 second was to long. well this fixed half of the problem, the light would move up but not down, and as it moved up the LED behind it would stay on. anyway i got it working, and ill just show u the code for you to see how i fixed it
code:

USE [ code][ /code]TAGS!

Change:

void loop(){
  val = digitalRead(sensorB);
if (val == HIGH) {
  state = state - 1;
}
delay(200);

To:

void loop(){
  val = digitalRead(sensorB);
if (val == HIGH) {
  delay(10);  //wait for any button bounce to stop
  while(digitalRead(sensorB);  //Do nothing while button is still pressed
  state = state - 1;
  if (state <0){ //make sure state can't go negative
    state = 0;
  }
}

Henry_Best:
I get the code thing now, sorry about that. I will change the way i delay my code later(I'm at work), but I wrote this today to loop my light movement from the end of the line to the start of the line, or if I had a circle of LEDs buy pressing one button it would go in one direction X amount a presses no matter how many time I pressed it

  val = digitalRead(sensorB);
  if (val == HIGH) {
    state = state - 1;
  }
  if (state <0) {
    state = 9
  }

would i have to write it like you did for it to work

  val = digitalRead(sensorB);
  if (val == HIGH) {
    state = state - 1;
    if (state <0){
      state = 9;
    }
  }

Also I don't know enough about the array code to work with it. I have the "Getting Started With Arduino" book
and it doesn't tell me how to use it, could you maybe post a link to somewhere I could learn, I would just look at YOUTUBE, but if you know of a better place please advise

could you maybe post a link to somewhere I could learn

http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Also I don't know enough about the array code to work with it. I have the "Getting Started With Arduino" book
and it doesn't tell me how to use it, could you maybe post a link to somewhere I could learn, I would just look at YOUTUBE, but if you know of a better place please advise

Here's an example of array usage -

Also good place to start...but usually very limited in scope —
http://arduino.cc/en/Reference/Array

Just like art, music, etc....programming often involves "sampling" or "borrowing" from others' work. One can also learn a lot by examining how others have done things. I've found a good place to browse for influence can be Github — online code repository. So, I'll do a google search for relevant code on that website. Something like, "site:github.com arduino array" in the google box. That's actually how I found the first example posted above.

As a small aside.... It seems you're going about learning a pretty good way! Try to figure out what works, do it the "hard way" first, then clean it up, refine it & use more advanced code/techniques. Good job. We typically learn A LOT more from our failures than our successes. Often, just trying something is the best way to figure it out! There isn't much you can do with (simple) code that can permanently damage your Arduino, which is nice!
Another tip - Save and compile often! Make a small change, try to compile, then save. Repeat. If doesn't compile, read the errors; figure out what they mean, and use them to teach yourself even more. If I'm making a large change or rewrite in a sketch, I'll save it under a new name. That way, I can easily revert back to previous version if I screw something up pretty bad. :blush:

(....I may have gotten a little off topic. Sorry.)

Good luck!

The_New_Guy:
Henry_Best:
I get the code thing now, sorry about that. I will change the way i delay my code later(I'm at work), but I wrote this today to loop my light movement from the end of the line to the start of the line, or if I had a circle of LEDs buy pressing one button it would go in one direction X amount a presses no matter how many time I pressed it

  val = digitalRead(sensorB);

if (val == HIGH) {
    state = state - 1;
  }
  if (state <0) {
    state = 9
  }

That will check if state is <0 every time through the loop.

would i have to write it like you did for it to work

  val = digitalRead(sensorB);

if (val == HIGH) {
    state = state - 1;
    if (state <0){
      state = 9;
    }
  }

That will only check if state <0 after state is decremented, the only time it could go below zero.

Also I don't know enough about the array code to work with it. I have the "Getting Started With Arduino" book
and it doesn't tell me how to use it, could you maybe post a link to somewhere I could learn, I would just look at YOUTUBE, but if you know of a better place please advise

Google 'Arduino array' for hundreds of examples, including YouTube. How you learn and who you learn from best is a personal choice that I cannot make for you. There's one guy (no names, no pack drill!) who, although very knowledgeable, I cannot listen to, as he's so slow and deliberate I get fed up waiting for him to explain what he's doing. Others, for me, go too fast, skipping casually over important bits. What suits you best is for you to find out.

Henry_best:

thanks for all the help. when i updated me code last night I just did in the way you did and it works fin so ill just leave it like this.
Also I have had similar experiences with watching videos but some time I just got to power though it so i don't miss anything.

next small project I'm going to start is a "mathematical door lock", where the user will have 5 buttons to use, each with different values either adding or subtracting X amount of values till i get to the value that will activate a or deactivate a servo motor or maybe a solenoid.

The_New_Guy:
Henry_best:

thanks for all the help. when i updated me code last night I just did in the way you did and it works fin so ill just leave it like this.
Also I have had similar experiences with watching videos but some time I just got to power though it so i don't miss anything.

next small project I'm going to start is a "mathematical door lock", where the user will have 5 buttons to use, each with different values either adding or subtracting X amount of values till i get to the value that will activate a or deactivate a servo motor or maybe a solenoid.

That doesn't sound very secure to me. Imagine your arithmetic is 1st button + 2nd button - 3rd button - 4th button + 5th button and your total (to open the lock) is 4. How many different ways can you input the numbers 1 to 5 to get 4 as your answer? I can see several without much effort. And increasing the number of button presses will make it LESS secure (more possible combs and perms will add up to the correct answer). Your best bet is to check the button sequence against a numerical 'password' stored in the Arduino's memory. The 'password' can be of any (reasonable) length to make 'cracking' it more difficult. Think 'combination padlock'. Using 3 numbers you've got 75 possible combinations (with 5 buttons), with 4 numbers: 375, with 5 numbers: 1875 and with 6 numbers: 9375. One correct answer in 9375 is fairly secure and with any more than 6 numbers to remember, it's more likely that the user will forget or incorrectly enter the code. You can add code to your sketch that will sound an alarm if more than, say, three wrong attempts are made.

Henry_Best:
That doesn't sound very secure to me. Imagine your arithmetic is 1st button + 2nd button - 3rd button - 4th button + 5th button and your total (to open the lock) is 4. How many different ways can you input the numbers 1 to 5 to get 4 as your answer? I can see several without much effort.

No offense, that sounds like hefty criticism based on a huge assumption. I can see how your example would hold even if the values of the buttons were different than "1,2,3,4,5". But why assume the "total" would be 4? Or even close?

What if button values were :
+336
-72
+426
+5
-294
And the magic total to unlock door were, say.... 5721 (which, nobody but the owner/user knows)??

What number of valid combinations exist?

1ChicagoDave:

Henry_Best:
That doesn't sound very secure to me. Imagine your arithmetic is 1st button + 2nd button - 3rd button - 4th button + 5th button and your total (to open the lock) is 4. How many different ways can you input the numbers 1 to 5 to get 4 as your answer? I can see several without much effort.

No offense, that sounds like hefty criticism based on a huge assumption. I can see how your example would hold even if the values of the buttons were different than "1,2,3,4,5". But why assume the "total" would be 4? Or even close?

What if button values were :
+336
-72
+426
+5
-294
And the magic total to unlock door were, say.... 5721 (which, nobody but the owner/user knows)??

What number of valid combinations exist?

Several, [see EDIT below] as it doesn't matter what order the buttons are pressed as long as the final total is correct.
1 + 2 + 3 + 4 + 5 = 5 + 4 + 3 + 2 + 1 = 3 + 5 + 2 + 4 + 1 = 4 + 1 + 3 + 5 + 2 etc., etc.
It doesnt matter how big or small the numbers are for that to be true.

EDIT. With 5 keys and 5 presses needed there are 5! (54321) = 120 correct combinations out of a possible 5^5 = 3125, a 1 in 26 chance of getting it correct =~4%. Keeping 5 buttons and increasing the number of presses needed will make that ratio even worse.
6 presses of 5 buttons will give 5*5! = 600 correct combs. 6^5 = 7776. A 1 in 13 chance of getting it correct =~8%.