button not working

I made a little reaction game and this is one test of it but when i press the button there happens nothing.

here is the code:

const int red = 0;
const int green = 1;
const int blue = 2;

const int led1 = 3;
const int led2 = 4;
const int led3 = 5;
const int led4 = 6;
const int led5 = 7;
const int led6 = 8;
const int led7 = 9;

const int button = 10;
int buttonstate = 0;

void setup() {

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);

pinMode(button, INPUT);
}

void loop()
 {
if ((buttonstate == HIGH) && (digitalRead(led4 == HIGH)))
{
  digitalWrite(blue, HIGH);
  delay(3000);
  digitalWrite(blue, LOW);
  digitalWrite(green, HIGH);
  delay(750);
  digitalWrite(green, LOW);
  delay(750);
  digitalWrite(green, HIGH);
  delay(750);
  digitalWrite(green, LOW);
  delay(750);
  digitalWrite(green, HIGH);
  delay(500);
  digitalWrite(green, LOW);
} else if
  ((buttonstate == HIGH) && (digitalRead(led4 == LOW)))
  {
  digitalWrite(blue, HIGH);
  delay(3000);
  digitalWrite(blue, LOW);
  digitalWrite(red, HIGH);
  delay(750);
  digitalWrite(red, LOW);
  delay(750);
  digitalWrite(red, HIGH);
  delay(750);
  digitalWrite(red, LOW);
  delay(750);
  digitalWrite(red, HIGH);
  delay(500);
  digitalWrite(red, LOW);
} else {
  /*digitalWrite(led1, HIGH);
  delay(750);
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  delay(750);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  delay(750);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  delay(750);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  delay(750);
  digitalWrite(led5, LOW);
  digitalWrite(led6, HIGH);
  delay(750);
  digitalWrite(led6, LOW);
  digitalWrite(led7, HIGH);
  delay(750);
  digitalWrite(led7, LOW);*/
}
 }

thx for the answers :sunglasses:

const int led4 = 6;


...if ((buttonstate == HIGH) && (digitalRead(led4 == HIGH)))

led4 is a constant. It's always equal to 6 and anything that's not zero will be considered "high" and it can never be low.

There might be other issues but that's what I noticed.

...The LED isn't always on, but the constant called 'led1' is always equal to 6. You'll need another variable to keep track of the state of that LED.

P.S.
Your "timing" probably won't work the way you expect because you can't read the button during the delay times. You'll only read the button at the beginning, and then after the delays, then again after the 2nd group of delays when it loops-around again.

You'll need to use the millis() technique from the Blink Without Delay Example so you can loop quickly (without delays) to read switch more often while checking millis() every-time through your quick-loop.

(deleted)

Hi,
How have you got your button wired?
If it is between pin 10 and 5V, do you have a pull down 10K resistor between pin 10 and gnd?

Tom… :slight_smile:

TomGeorge:
Hi,
How have you got your button wired?
If it is between pin 10 and 5V, do you have a pull down 10K resistor between pin 10 and gnd?

Tom.. :slight_smile:

DVDdoug:

const int led4 = 6;

...if ((buttonstate == HIGH) && (digitalRead(led4 == HIGH)))




**P.S.**
**Your "timing" probably won't work the way you expect** because you can't read the button during the delay times. You'll only read the button at the beginning, and then after the delays, then again after the 2nd group of delays when it loops-around again. 

You'll need to use the millis() technique from the Blink Without Delay Example so you can loop quickly (without delays) to read switch more often while checking millis() every-time through your quick-loop.

I know about the timing problem i stil going to change it.
And yes i have the resistor.

thx for the reply

Please post your latest code here with the corrections already suggested in this thread.

How are you getting on using millis() for timing ?

Have you read Using millis() for timing. A beginners guide, Several things at the same time and looked at the BlinkWithoutDelay example in the IDE.

Hi,
Have you got code just to test your button?

Tom... :slight_smile:

Hi,
i'am doing a little project it's a reaction game,
there need to blink 5 leds like in knightrider and if you push the button when the led
in the middle is on the rgb led will turn green or else it will turn red.
but the leds are not blinking, i did check if the leds do work and, they work all.
so i'am asking for a little help.

here is the code:

const int button = 7;
const int ledg = 0;
const int ledr = 1;
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int led5 = 6;

int ledstate = 0;

int buttonstate = 0;

long previous = 0;
long interval = 1000;

int ledOn = 0;

void setup() {
  pinMode(button, INPUT);
  pinMode(ledg, OUTPUT);
  pinMode(ledr, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}
void loop() {
  ledstate = digitalRead(led3);
  buttonstate = digitalRead(button);
  unsigned long currentmillis = millis();
  if (currentmillis == interval) {
    ledOn + 1;
    interval + 1000;
  }
  if (ledOn >= 7) {
    ledOn = 0;
  }
  if (ledOn == 1) {
    digitalWrite(led5, LOW);
    digitalWrite(led1, HIGH);
  }
  if (ledOn == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
  }
  if (ledOn == 3) {
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (ledOn == 4) {
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
  }
  if (ledOn == 5) {
    digitalWrite(led4, LOW);
    digitalWrite(led5, HIGH);
  }
  if (buttonstate == HIGH && ledstate == HIGH) {
    digitalWrite(ledg, HIGH);
    delay(2000);
    digitalWrite(ledg, LOW);
  }
  if (buttonstate == HIGH && ledstate == LOW) {
    digitalWrite(ledr, HIGH);
    delay(2000);
    digitalWrite(ledr, LOW);
  }
}

well after a long time i started working on it again,
but the problem is the leds are not turning on,
i did chek if the leds do work and they work

here is the code:

const int button = 7;
const int ledg = 0;
const int ledr = 1;
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int led5 = 6;

int ledstate = 0;

int buttonstate = 0;

long previous = 0;
long interval = 1000;

int ledOn = 0;

void setup() {
  pinMode(button, INPUT);
  pinMode(ledg, OUTPUT);
  pinMode(ledr, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}
void loop() {
  ledstate = digitalRead(led3);
  buttonstate = digitalRead(button);
  unsigned long currentmillis = millis();
  if (currentmillis == interval) {
    ledOn + 1;
    interval + 1000;
  }
  if (ledOn >= 7) {
    ledOn = 0;
  }
  if (ledOn == 1) {
    digitalWrite(led5, LOW);
    digitalWrite(led1, HIGH);
  }
  if (ledOn == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
  }
  if (ledOn == 3) {
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (ledOn == 4) {
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
  }
  if (ledOn == 5) {
    digitalWrite(led4, LOW);
    digitalWrite(led5, HIGH);
  }
  if (buttonstate == HIGH && ledstate == HIGH) {
    digitalWrite(ledg, HIGH);
    delay(2000);
    digitalWrite(ledg, LOW);
  }
  if (buttonstate == HIGH && ledstate == LOW) {
    digitalWrite(ledr, HIGH);
    delay(2000);
    digitalWrite(ledr, LOW);
  }
}

What is this code supposed to do?

ledOn + 1;
interval + 1000;

ledon is for turning on the leds when ledon is 1 then led 1 needs to be on when 2 then led 2 on etc..
+1000 in interval is when there needs something to be added to ledon.
(sorry if you don't understand it i'am not so good in explaining)

ledOn + 1;

Did you mean

 ledOn = ledOn +1;

Because what you wrote does nothing. It adds one to ledOn and then throws the result away. The compiler probably just removes that line since it is useless.

OP started a new post for same topic here: [MERGED] problem with program - Project Guidance - Arduino Forum

Reported to mods

That's not how you use millis() for timing. Have a look at any of the good tutorials you were pointed to in post #5 and see if you can spot the differences.

Steve

Hi,

const int button = 7;
const int ledg = 0;
const int ledr = 1;
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int led5 = 6;

To help make your code readable this may help;

const int buttonPin = 7;
const int ledgPin = 0;
const int ledrPin = 1;
const int led1Pin = 2;
const int led2Pin = 3;
const int led3Pin = 4;
const int led4Pin = 5;
const int led5Pin = 6;

In the IDE look for Files, Examples, Digital, Blink Without Delay.
It will give you an example of using millis() to create a delay.

Tom.... :slight_smile: