Go Down

Topic: LED Bar graph and Button (Read 19030 times) previous topic - next topic

BulldogLowell

can you post your current code?  the program you are using with the beep sounds?

thanks.

jameskirk

Just got home here is the code I used with the tone...
Oh it still does it with the tone not in...
Code: [Select]
#define PHASER 0
#define DIAGNOSTIC 1

int ledPin[10] = {12,11,10,9,8,6,5,4,3,2};
int scanArray[5] = {0b1000000001, 0b0100000010, 0b0010000100, 0b0001001000, 0b0000110000};
const int pushButton  = 7;
const int pushButton2 = 13;
const int pushButton3 = A1;
int spkr = A0;
int lastVal;
int lastVal2;
int lastVal3;
int index = 0;
byte state = PHASER;// initialises in phaser state
unsigned long timeStamp;

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 11; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
  pinMode(pushButton,INPUT);
  pinMode(pushButton2,INPUT);
  pinMode(pushButton3,INPUT);
}
void loop()
{
  //
  int val=digitalRead(pushButton);
  delay(50); // Software debouncing
  if(val==HIGH && lastVal == LOW && state == PHASER)
  {
    timeStamp = millis();
    index <<= 1;
    index |= 1;
    if (index > 0b1111111111) index = 0b1111111111;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
tone(spkr, 2000, 50);
    }
  }
  else if (val==LOW && lastVal == HIGH)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
    }
  }
  /*
  else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    state = PHASER;
  }*/
  lastVal = val;
  //
  int val2 = digitalRead(pushButton2);
  delay(50); // Software debouncing
  if(val2 == HIGH && lastVal2 == LOW && state == PHASER)
  {
    index >>= 1;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
tone(spkr, 2000, 50);
    }
  }
  lastVal2 = val2;
  // Keep this here for now................
  int val3 = digitalRead(pushButton3);
  delay(50);
  if (val3 == HIGH && lastVal3 == LOW)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
  }
  if (state == DIAGNOSTIC)
  {
    diagnogsticScan();
    // other diagnostic stuff here...
  }
}

void diagnogsticScan()
{
  static unsigned long scanStart;
  static byte scanIndex = 0;
  static byte increment = 1;
  if (millis() - scanStart >= 60UL)
  {
    Serial.println(scanIndex);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(scanArray[scanIndex], i));
      Serial.println(i);
    }
    scanIndex += increment;
    if (scanIndex == 0 || scanIndex == 4)
    {
      increment = -increment;
    }
    scanStart = millis();
  }
}

Thank You
Michael

BulldogLowell

I think the problem is that the LedPin lites on reset, that may be affecting it

I changed this:

Code: [Select]
const int pushButton2 = 13;

to this:

Code: [Select]
const int pushButton2 = 1;

and commented out Serial:

Code: [Select]
//Serial.begin(115200);

jameskirk

#33
Oct 15, 2014, 01:21 am Last Edit: Oct 15, 2014, 01:23 am by jameskirk Reason: 1
It made things worse when it comes on it beeps once and then starts the scroll blinking and when
you push the up button it is fine but the down button don't work...
If I put things in the right place that you wanted here is the code...
Code: [Select]
#define PHASER 0
#define DIAGNOSTIC 1

int ledPin[10] = {12,11,10,9,8,6,5,4,3,2};
int scanArray[5] = {0b1000000001, 0b0100000010, 0b0010000100, 0b0001001000, 0b0000110000};
const int pushButton  = 7;
const int pushButton2 = 1;
const int pushButton3 = A1;
int spkr = A0;
int lastVal;
int lastVal2;
int lastVal3;
int index = 0;
byte state = PHASER;// initialises in phaser state
unsigned long timeStamp;

void setup()
{
  //Serial.begin(115200);
  for (int i = 0; i < 11; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
  pinMode(pushButton,INPUT);
  pinMode(pushButton2,INPUT);
  pinMode(pushButton3,INPUT);
}
void loop()
{
  //
  int val=digitalRead(pushButton);
  delay(50); // Software debouncing
  if(val==HIGH && lastVal == LOW && state == PHASER)
  {
    timeStamp = millis();
    index <<= 1;
    index |= 1;
    if (index > 0b1111111111) index = 0b1111111111;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
tone(spkr, 2000, 50);
    }
  }
  else if (val==LOW && lastVal == HIGH)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
    }
  }
  /*
  else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    state = PHASER;
  }*/
  lastVal = val;
  //
  int val2 = digitalRead(pushButton2);
  delay(50); // Software debouncing
  if(val2 == HIGH && lastVal2 == LOW && state == PHASER)
  {
    index >>= 1;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
tone(spkr, 2000, 50);
    }
  }
  lastVal2 = val2;
  // Keep this here for now................
  int val3 = digitalRead(pushButton3);
  delay(50);
  if (val3 == HIGH && lastVal3 == LOW)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
  }
  if (state == DIAGNOSTIC)
  {
    diagnogsticScan();
    // other diagnostic stuff here...
  }
}

void diagnogsticScan()
{
  static unsigned long scanStart;
  static byte scanIndex = 0;
  static byte increment = 1;
  if (millis() - scanStart >= 60UL)
  {
    Serial.println(scanIndex);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(scanArray[scanIndex], i));
      Serial.println(i);
    }
    scanIndex += increment;
    if (scanIndex == 0 || scanIndex == 4)
    {
      increment = -increment;
    }
    scanStart = millis();
  }
}

Thanks
Michael

BulldogLowell

Hmmm 

It works fine for me, though I am using internal pull-up resistors and inverted the logic.

I'll post my code later.

BulldogLowell

this works well for me... notice the use of the internal pull-ups.

how are your pushbuttons wired?


Code: [Select]
#define PHASER 0
#define DIAGNOSTIC 1

int ledPin[10] = {
  12,11,10,9,8,6,5,4,3,2};
int scanArray[5] = {
  0b1000000001, 0b0100000010, 0b0010000100, 0b0001001000, 0b0000110000};
const int pushButton  = 7;
const int pushButton2 = 1;
const int pushButton3 = A1;
int spkr = A0;
int lastVal;
int lastVal2;
int lastVal3;
int index = 0;
byte state = PHASER;// initialises in phaser state
unsigned long timeStamp;

void setup()
{
  //Serial.begin(115200);
  for (int i = 0; i < 11; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
  pinMode(pushButton,INPUT_PULLUP);
  pinMode(pushButton2,INPUT_PULLUP);
  pinMode(pushButton3,INPUT);
}
void loop()
{
  //
  int val=digitalRead(pushButton);
  delay(50); // Software debouncing
  if(val==LOW && lastVal == HIGH && state == PHASER)
  {
    timeStamp = millis();
    index <<= 1;
    index |= 1;
    if (index > 0b1111111111) index = 0b1111111111;
    Serial.print("index = ");
    Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
      tone(spkr, 2000, 50);
    }
  }
  else if (val==HIGH && lastVal == LOW)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      for (int i = 0; i < 10; i++)
      {
        digitalWrite (ledPin[i], LOW);
      }
      if (state == DIAGNOSTIC) state = PHASER;
      else state = DIAGNOSTIC;
    }
  }
  /*
  else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
   {
   for (int i = 0; i < 10; i++)
   {
   digitalWrite (ledPin[i], LOW);
   }
   state = PHASER;
   }*/
  lastVal = val;
  //
  int val2 = digitalRead(pushButton2);
  delay(50); // Software debouncing
  if(val2 == LOW && lastVal2 == HIGH && state == PHASER)
  {
    index >>= 1;
    Serial.print("index = ");
    Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
      tone(spkr, 2000, 50);
    }
  }
  lastVal2 = val2;
  // Keep this here for now................
  int val3 = digitalRead(pushButton3);
  delay(50);
  if (val3 == HIGH && lastVal3 == LOW)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
  }
  if (state == DIAGNOSTIC)
  {
    diagnogsticScan();
    // other diagnostic stuff here...
  }
}

void diagnogsticScan()
{
  static unsigned long scanStart;
  static byte scanIndex = 0;
  static byte increment = 1;
  if (millis() - scanStart >= 60UL)
  {
    Serial.println(scanIndex);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(scanArray[scanIndex], i));
      Serial.println(i);
    }
    scanIndex += increment;
    if (scanIndex == 0 || scanIndex == 4)
    {
      increment = -increment;
    }
    scanStart = millis();
  }
}

jameskirk

It is still messed up like before I can't use the down button and I can't switch to the diagnostic mode...
Here is my button Pic's...


Thanks
Michael

BulldogLowell

hmmm

did you try using the pull-ups and my code?


jameskirk

I copied the sketch you posted just a reminder when you say pullup resistor you mean in the sketch
not on the breadboard correct???


I found the problem...
I removed the 3rd switch from the breadboard and that's when all the problems started I reinstalled it now
there is no random blinking...
I tried removing it from the sketch but that messes everything up too...
I used the last good sketch before anything went wrong and it works...
But I still have that 3rd switch now...
Thanks
Michael

BulldogLowell


jameskirk

I need to get rid of the 3rd switch and remove it from the sketch...

BulldogLowell

well, don't you know I think I found the problem... but you are getting rid of the third switch already...

I forgot to put into the code:

Code: [Select]
int val3 = digitalRead(pushButton3);
  delay(50);
  if (val3 == HIGH && lastVal3 == LOW)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
  }
  lastVal3 = val3;//<<<<<<<<<<<<<<<<<<<<<<<<<<<< this line, right?
  if (state == DIAGNOSTIC)


but you should be able to ditch that whole block of code, no problem since you are not using that hardware.

jameskirk

Well I removed what you said and I took out the...
Code: [Select]
const int pushButton3 = A1;
Code: [Select]
int lastVal3;
Code: [Select]
// Keep this here for now................
  int val3 = digitalRead(pushButton3);
  delay(50);
  if (val3 == HIGH && lastVal3 == LOW)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPin[i], LOW);
    }
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
  }
  if (state == DIAGNOSTIC)

Now nothing works again...

BulldogLowell

don't take this out:

Code: [Select]
if (state == DIAGNOSTIC)

jameskirk

OK that worked (so far) lol...
Thanks
Michael

Go Up