Go Down

Topic: Stop the light (Read 402 times) previous topic - next topic

Koepel

#15
Dec 13, 2015, 01:45 am Last Edit: Dec 13, 2015, 01:46 am by Koepel
BulldogLowell, you don't have to use 'byte' instead of an 'int' so often. The compiler will take care of many of those. For constants, they will be put directly in the code, without using a variable.

When the array myLed[] is changed from 'byte' to 'int', that increases the code.
However when the 'inputPin', 'buttonPin', 'buttonState', 'oldState' are made 'int' the code size stays the same.
For an example sketch for someone new, it is better to use 'int'. Such advanced optimizations is not the first concern.

BulldogLowell

BulldogLowell, you don't have to use 'byte' instead of an 'int' so often....
...Such advanced optimizations is not the first concern.
Actually, I can use whatever the heck I want to use.   >:(

Learning by example works for me so I like to show examples.

IMHO, I benefited a lot from folks here beating these minor fundamentals into my code early on.

For me, un-learning bad habits can be harder than changing one's name.

BulldogLowell

Code: [Select]
byte oldState = HIGH;

You got it not const now, but it should probably be static if you really want it to represent the state of the button from the last loop.
just can't catch a break!!!


insignia

#18
Dec 13, 2015, 03:53 pm Last Edit: Dec 13, 2015, 03:55 pm by insignia
still led issues tho.. starts at the second light after the initial blink 3x startup for new game.. the button still not working correctly and the leds do not move button press is 1 led off..

Code: [Select]

#define NUM_LEDS 7

const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};
const byte inputPin = 9;

//button
byte buttonPin = 9;
byte buttonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP);
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop()
{
  static unsigned long lastMillis;
  static byte activePin = 0;
  const static unsigned long duration = 1000UL;
  static byte indexer = 1;
  static byte oldState = HIGH; // thanks Delta_G!!!
  if (millis() - lastMillis >= duration)
  {
    for (byte i = 0; i < NUM_LEDS; i++)
    {
      if (activePin == i)
      {
        digitalWrite(myLed[i], HIGH);
      }
      else
      {
        digitalWrite(myLed[i], LOW);
      }
      Serial.print(activePin == i? "X" : "O");
    }
    Serial.println();
    activePin += indexer;
    if (activePin == 0 || activePin == NUM_LEDS - 1)
    {
      indexer *= -1;
    }
    lastMillis += duration;
  }
  // button read bits..
  byte state = digitalRead(inputPin);
  if (oldState == HIGH && state == LOW)
  {
    blinkThreeTimes(activePin);
  }
  oldState = state;
  delay(30);  //crude debounce
  //
}

void blinkThreeTimes(byte pin)
{
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], j == pin? HIGH : LOW);
      if(j == pin) Serial.println(pin);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}



i'm gonna have to play with this for awhile.. not sure what all the "serial" stuff is about... but thanks everyone
Whenever I say "Standalone" - I mean whatever makes it work has to be put onto a perfboard run by a pro mini - so it is a true standalone unit without my actual Arduino mega 2560 attached to make it work..

BulldogLowell

i'm gonna have to play with this for awhile.. not sure what all the "serial" stuff is about... but thanks everyone
Use your serial monitor and see for yourself...

insignia

#20
Dec 14, 2015, 01:51 am Last Edit: Dec 14, 2015, 01:59 am by insignia
i went over it, but i cannot see the issue.. but here's what it does:

all 7 leds blink 3x to show start of new game.. CORRECT

then it does this...

second led blinks 3x..INCORRECT - it shouldn't blink 3x at this point and it should start with first light and go in sequence and then back.. so on and so forth..

then, after the second led blinks 3x it then does this..

moves quickly to the sixth light, then follows through with the sequence - if left alone..

but if i press the button at any time.. the button press is one led off.. it blinks the lit led 3x and then it speeds up in sequence and for 4 leds and then slows back down in sequence thing.. this happens every time the button is pressed :/

my wiring diagram:



Code: [Select]


#define NUM_LEDS 7

const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};
const byte inputPin = 9;

//button
//byte buttonPin = 9;...........................no need for this to be here since it's declared above
byte buttonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP);
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop()
{
  static unsigned long lastMillis;
  static byte activePin = 0;
  const static unsigned long duration = 1000UL;
  static byte indexer = 1;
  static byte oldState = HIGH; // thanks Delta_G!!!
  if (millis() - lastMillis >= duration)
  {
    for (byte i = 0; i < NUM_LEDS; i++)
    {
      if (activePin == i)
      {
        digitalWrite(myLed[i], HIGH);
      }
      else
      {
        digitalWrite(myLed[i], LOW);
      }
      Serial.print(activePin == i ? "X" : "O");
    }
    Serial.println();
    activePin += indexer;
    if (activePin == 0 || activePin == NUM_LEDS - 1)
    {
      indexer *= -1;
    }
    lastMillis += duration;
  }
  // button read bits..
  byte state = digitalRead(inputPin);
  if (oldState == HIGH && state == LOW)
  {
    blinkThreeTimes(activePin);
  }
  oldState = state;
  delay(30);  //crude debounce
  //
}

void blinkThreeTimes(byte pin)
{
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], j == pin ? HIGH : LOW);
      if (j == pin) Serial.println(pin);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}


Whenever I say "Standalone" - I mean whatever makes it work has to be put onto a perfboard run by a pro mini - so it is a true standalone unit without my actual Arduino mega 2560 attached to make it work..

BulldogLowell

#21
Dec 14, 2015, 02:09 am Last Edit: Dec 14, 2015, 02:09 am by BulldogLowell
Pushbutton should be wired to 9 and Ground, not 5V.

insignia

no, the button is connected correctly.. i attempted ground and the button is useless that way. connected the way i showed - it works..

the leds are not working properly..
Whenever I say "Standalone" - I mean whatever makes it work has to be put onto a perfboard run by a pro mini - so it is a true standalone unit without my actual Arduino mega 2560 attached to make it work..

BulldogLowell

#23
Dec 14, 2015, 02:26 am Last Edit: Dec 14, 2015, 02:28 am by BulldogLowell
no, the button is connected correctly.. i attempted ground and the button is useless that way. connected the way i showed - it works..

the leds are not working properly..
I'm sorry but you are not correct.

The logic in the code I gave you depends on the pin (which is set HIGH using INPUT_PULLUP) to detect the edge of the button press when it is shorted to ground.  That is the way the code is written.  If you are connecting the pin to 5V, yes the program will not function the way I wrote it (albeit perhaps not what you actually want).

and, No resistor is needed on the pushbutton....




Delta_G

#24
Dec 14, 2015, 02:26 am Last Edit: Dec 14, 2015, 02:29 am by Delta_G
OK, if you want the button to be connected to 0V when not pushed and 5V when it is pushed (the way you have it wired in your picture) then is this line correct?  Do you know what this line is doing?  If not, then you need to learn

Code: [Select]
pinMode(inputPin, INPUT_PULLUP);


And is this line correct for the way you have it wired?  With it wired that way what will a button that has just been pushed read?  What would have read last run through if it wasn't pushed? 

Code: [Select]
if (oldState == HIGH && state == LOW)


It's time to start trying to understand some of this code instead of just getting Bulldog to write it for you.  If your project doesn't work, then take that as an opportunity to learn a few things.  It will work when you do. 
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

insignia

#25
Dec 14, 2015, 02:32 am Last Edit: Dec 14, 2015, 02:34 am by insignia
I'm sorry but you are not correct.

The logic in the code I gave you depends on the pin (which is set HIGH using INPUT_PULLUP) to detect the edge of the button press when it is shorted to ground.  That is the way the code is written.  If you are connecting the pin to 5V, yes the program will not function the way I wrote it (albeit perhaps not what you actually want).

and, No resistor is needed on the pushbutton....




yeah, after i posted and attempted removing the resistor on the button and back to gnd.. yes, now it works.. however, the leds are still not working properly..

see #20

well, now it speeds up 3 lights after every press..
Whenever I say "Standalone" - I mean whatever makes it work has to be put onto a perfboard run by a pro mini - so it is a true standalone unit without my actual Arduino mega 2560 attached to make it work..

BulldogLowell

from your OP:

Code: [Select]
 //When the player presses the button...
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) { // if the button is pressed

    // blink the lit led 3x to show which one was lit when button was pressed
// HERE IS WHERE I NEED THE HELP...........................................

// and then keep going through the sequence

so what do you want it to do, and what have you tried besides posting your results.

Have you tried to put some Serial.print() messages in there to figure out what's what?

BulldogLowell

reseting the millis() timer in the blinkThreeTimes() function eliminates the 'catch-up' blinking...

(moved static vars to global)

Code: [Select]
#define NUM_LEDS 7

const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};
const byte inputPin = 9;

//button
byte buttonPin = 9;
byte buttonState = 0;

unsigned long lastMillis;
byte activePin = 0;
unsigned long duration = 1000UL;
byte indexer = 1;
byte oldState = HIGH; // thanks Delta_G!!!

void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP);
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop()
{
  if (millis() - lastMillis >= duration)
  {
    for (byte i = 0; i < NUM_LEDS; i++)
    {
      if (activePin == i)
      {
        digitalWrite(myLed[i], HIGH);
      }
      else
      {
        digitalWrite(myLed[i], LOW);
      }
      Serial.print(activePin/* == i? "X" : "O"*/);
    }
    Serial.println();
    activePin += indexer;
    if (activePin == 0 || activePin == NUM_LEDS - 1)
    {
      indexer *= -1;
    }
    lastMillis += duration;
  }
  // button read bits..
  byte state = digitalRead(inputPin);
  if (oldState == HIGH && state == LOW)
  {
    blinkThreeTimes(activePin);
  }
  oldState = state;
  delay(30);  //crude debounce
  //
}

void blinkThreeTimes(byte pin)
{
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], j == pin ? HIGH : LOW);
      if (j == pin) Serial.println(pin);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
  lastMillis = millis(); //<<<<<<<<<<< reset the timer
}

insignia

#28
Dec 14, 2015, 02:57 am Last Edit: Dec 14, 2015, 03:01 am by insignia
what it's supposed to do:

1.) all 7 leds blink 3x at start
2.) delay for a sec
3.) lights go back and forth in sequence - (like knight rider lights)
4.) when button is pressed - the led that is lit is to blink 3x quickly

if a red led (first 3 or last 3) are lit... see #4 and start the sequence from the beginning..(basically start a new game.. start from the beginning.. #1

if the middle led (blue) then see #4 but continue is in sequence.. just a little faster - this continues until a red led is caught..

right now with yer last adjust code, at start of #3 it speeds up the first 3 leds right off


i'll look back in tomorrow.. gotta get ready for work.. i hate working overnights :/
Whenever I say "Standalone" - I mean whatever makes it work has to be put onto a perfboard run by a pro mini - so it is a true standalone unit without my actual Arduino mega 2560 attached to make it work..

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy