Arduino Custom Flasher Box for LED Light Bar

Thread Starter

Blenderite

Joined Feb 2, 2016
16
Alright so that video has actually turned out to be the most helpful so far. My only question, he says that the internal Arduino reference as the input voltage (see the diagram at 5:18). I think I am going to have to use that option because all my digital pins are being used for the LED's. The problem is that he doesn't say how to use the internal Arduino reference voltage, unless I missed it. I did a quick google search and didn't see anything about that.
 

BillB3857

Joined Feb 28, 2009
2,573
Some of the other links I posted show how to set registers to enable the external reference. The last link I posted has some example code that has a lot of remarks in it.
 

Thread Starter

Blenderite

Joined Feb 2, 2016
16
Ok, so I understand that now. Now what I need to know, can I utilize the same pin for an interrupt as I am using to read the value from the keypad?
 

BillB3857

Joined Feb 28, 2009
2,573
The way I read it, the conversion from analog to digital will generate the interrupt upon completion. Your code would need to use that interrupt to do whatever you want. I could be totally off base here and would hope someone else would jump in and correct my assumptions.
 

Sensacell

Joined Jun 19, 2012
3,786
An interrupt is not appropriate here, it's an unnecessary complication.
The keyboard outputs a voltage level, you must sample it constantly with the ADC to determine the current state.

What you need to do is place the ADC reading code inside your delay function, so whenever it's in the delay (most of the time) it's going to be reading the analog input value. You might find that you need to validate the voltage by sampling it several times to detect a valid button press. While the voltage is changing, the ADC might read some invalid indeterminate voltage levels.

While it is easy to connect, I would never use that kind of "voltage" keyboard, seems like a nightmarish device to me, fraught with potential problems.
 

Thread Starter

Blenderite

Joined Feb 2, 2016
16
After a lottle bit of thought, I am going to try using a potentiometer to select which pattern is used. I think this should be a lot easier than this keypad.
 

Sensacell

Joined Jun 19, 2012
3,786
How about a single push button to change the mode? Just increment a counter every time the button is pressed.
De-bounce the switch in code.

Simple, you don't need to mess with the vagaries of the ADC, analog noise, etc.
 

Hutkikz

Joined Jan 29, 2016
9
The problem with the delay() function on the arduino is that it stops everything ( like reading inputs ) until it's over.
The way to fix that is to use timing code instead.
Like a stopwatch it keeps checking the time. and when the time is up increment a counter to move to next step in the pattern.
this gives it plenty of time to read inputs.

I felt bad for posting code earlier and not properly explaining it.
so I wrote an smaller example thats fully commented and hopefully easier to understand.
I used a button here but you could use a pot. or that keypad just as easily.
if it still isn't clear just ask and I'll try to explain better.
Code:
const byte led1 = 9;  // led1 pin
const byte led2 = 10;  // led2 pin
const byte button = 5;  // whatever pin the button is on

byte pattern;  // counter to track which pattern we're on
byte patternStep;  // counter to track which step we're at

unsigned long startTime = 0;  // This variable starts the timer
const unsigned long baseInterval = 1000; // Base Timing Interval ( so we can reset the interval if it was modified )
unsigned long interval = 1000;  // Actual Timing Interval ( can be modified )


void setup()
{
  pinMode (button, INPUT_PULLUP);  // sets pull up resistor - connect button from pin and ground
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
}

void loop()
{
  if (digitalRead(button) == LOW)  // if button has been pressed adds 1 to the pattern counter
  {
  pattern++;  // increment pattern
  if (pattern > 1)  // reset pattern to 0 if it is maxed
  {
  pattern = 0;
  }
  interval = baseInterval;  // resets if the last time period was modified
  patternStep = 0;  // reset pattern step
  startTime = millis();  // start the timer
  digitalWrite (led1, LOW);  // reset all led's low
  digitalWrite (led2, LOW);
  }

  /////////////// TIMING CODE \\\\\\\\\\\\\\\\\\\\\

  unsigned long currentTime = millis();  // record current time
  if (currentTime - startTime >= interval)  // if time period has elapsed
  {
  patternStep++;  // increment patternStep
  startTime = millis();  // restart the Timer
  interval = baseInterval;  // resets interval if the last time period was modified
  }


  if (pattern == 0)  // computers start counting at 0
  {
  pattern0();  // calls pattern 0 function
  }
  if (pattern == 1)
  {
  pattern1();  // calls pattern 1 function
  }
}


void pattern0()  // this pattern alternately flashes two led's
{
  if (patternStep == 0)  // step 0
  {
  digitalWrite(led1, HIGH); // set the lights for this step
  digitalWrite(led2, LOW);
  }
  if (patternStep == 1)  // step 1
  {
  digitalWrite(led1, LOW);  // set the lights for this step
  digitalWrite(led2, HIGH);
  patternStep = 0;  // reset pattern step
  }
}


void pattern1()  // this pattern both led's alternate long flash then short flash
{
  switch (patternStep) // a switch statement is like a bunch of if statement's but less wordy
  {
  case 0:  // step 0
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  break;

  case 1:  // step 1
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  break;

  case 2:  // step 2
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  interval = 500;  // modify interval for this step
  break;

  case 3:  // step 3
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  interval = 500;  // modify interval for this step
  patternStep = 0;  // reset pattern step
  break;
  }
}
 
Last edited:
Top