Hello,
First post here so please be gentle...
I thought I would ask for some help because I've spent a couple of frustrating days trying to figure out what's going on.
So, I have the following code, which seems to work fine when I use two (momentary) switches (A0, A1).
The idea is that I have 11 LEDs (D2-D12) and A0 selects a "range" from D2 to D12, while A1 will cycle from D2 all the way up to the selected range.
So, for example, if I use A0 to select the pin D7, the A1 will cycle from D2 to D7. I hope this makes sense... Here's the code:
// constants won't change. They're used here to set pin numbers:
const int range_button = A0; // Button to select the range
const int scan_button = A1; // Button to strt the scanning
const int array_PINS[] = {13, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // Pin13 is used as "dummy" pin for the bit-sifting
const int array_HEX[] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800};
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long previousMillis = 0; // Will be used to track how long since last event "fired"
unsigned long currentMillis = 0;
unsigned long scan_interval = 450; // (ms) Time intervals between LEDs
unsigned long rng_interval = 200; // (ms) Time intervals for the range button
// Variables will change
int LEDstate = 0x01;
int rng_LEDstate = 0x01;
// scanner() variables
int scan_counter = 0; // counter for the number of button presses
int scan_buttonState = 0; // current state of the button
int scan_lastButtonState = 0; // previous state of the button
int idx = 0; // array index
// Calling functions
void range();
void scanner();
void pins_state();
void setup()
{
pinMode(range_button, INPUT);
pinMode(scan_button, INPUT);
// Set Pins with LEDs to OUTPUT. Includes Pin13
for (int x = 0; x < sizeof(array_PINS); x++) {
pinMode(array_PINS[x], OUTPUT);
}
}
void loop() {
range();
scanner();
}
//---------------------------------------------------------------------------
//--------------------------------- FUNCTIONS -------------------------------
//---------------------------------------------------------------------------
// Function to select the number of steps/range of the LEDs
void range()
{
currentMillis = millis();
if (digitalRead(range_button) == HIGH)
{
if (idx < 12) {
if ((currentMillis - previousMillis) >= rng_interval) {
previousMillis = currentMillis;
// Use "<<" to "bit-shift" everything to the left once
rng_LEDstate = rng_LEDstate << 1;
idx++;
digitalWrite(array_PINS[idx], bitRead(rng_LEDstate, idx));
digitalWrite(array_PINS[idx - 1], bitRead(rng_LEDstate, idx - 1));
}
} else {
rng_LEDstate = 0x1;
idx = 0;
digitalWrite(13, HIGH);
}
}
}
//---------------------------------------------------------------------------
// Function to scan through the selected steps/range of the LEDs
void scanner()
{
scan_buttonState = digitalRead(scan_button);
if (scan_buttonState != scan_lastButtonState)
{
if (scan_buttonState == HIGH)
{
scan_counter++;
delay(50); // Delay a little bit to avoid bouncing
}
}
scan_lastButtonState = scan_buttonState;
if (scan_counter % 2 == 1 && LEDstate <= array_HEX[idx]) {
pins_state();
for (int x = 0; x <= idx; x++) {
digitalWrite(array_PINS[x], bitRead(LEDstate, x));
}
} else {
LEDstate = 0x1;
scan_counter = 0;
digitalWrite(13, HIGH);
}
}
//---------------------------------------------------------------------------
// Function to turn ON/OFF the LEDs
void pins_state()
{
currentMillis = millis();
if ((currentMillis - previousMillis) >= scan_interval) {
previousMillis = currentMillis;
// Use "<<" to "bit-shift" everything to the left once
LEDstate = LEDstate << 1;
}
}
What I've been trying to do (but I have failed miserably) is to include the OneButton.h and use only A0 for both tasks. I have managed to use the range() function with a long press but the scanner() seems to not working properly (sort press). Here's what I've got so far:
#include "OneButton.h"
OneButton button(A1, false, false);
const int array_PINS[] = {13, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // Pin13 is used as "dummy" pin for the bit-sifting
const int array_HEX[] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800};
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long previousMillis = 0; // Will be used to track how long since last event "fired"
unsigned long currentMillis = 0;
unsigned long scan_interval = 450; // (ms) Time intervals between LEDs/ Relays
unsigned long rng_interval = 200; // (ms) Time intervals for the range button
// Variables will change
int LEDstate = 0x01;
int rng_LEDstate = 0x01;
// scanner() variables
int scan_counter = 0; // counter for the number of button presses
int scan_buttonState = 0; // current state of the button
int scan_lastButtonState = 0; // previous state of the button
int idx = 0; // array index
// Calling functions
void range();
void scanner();
void pins_state();
void setup()
{
button.attachClick(scanner); // link the function to be called on a singleclick event.
button.attachDuringLongPress(range); // link the function to be called on a longpress event.
button.setPressTicks(1000); // set time (ms) before long press triggers the function
// Set Pins with LEDs to OUTPUT. Includes Pin13
for (int x = 0; x < sizeof(array_PINS); x++) {
pinMode(array_PINS[x], OUTPUT);
}
}
void loop() {
button.tick();
delay(10); // not sure if this is needed
}
//---------------------------------------------------------------------------
//--------------------------------- FUNCTIONS -------------------------------
//---------------------------------------------------------------------------
// Function to select the number of steps/range of the LEDs/ Relays
void range()
{
currentMillis = millis();
//if (digitalRead(A1) == HIGH)
//{
if (idx < 12) {
if ((currentMillis - previousMillis) >= rng_interval) {
previousMillis = currentMillis;
// Use "<<" to "bit-shift" everything to the left once
rng_LEDstate = rng_LEDstate << 1;
idx++;
digitalWrite(array_PINS[idx], bitRead(rng_LEDstate, idx));
digitalWrite(array_PINS[idx - 1], bitRead(rng_LEDstate, idx - 1));
}
} else {
rng_LEDstate = 0x1;
idx = 0;
digitalWrite(13, HIGH);
}
//}
}
//---------------------------------------------------------------------------
// Function to scan through the selected steps/range of the LEDs/ Relays
void scanner()
{
scan_buttonState = digitalRead(A1);
if (scan_buttonState != scan_lastButtonState)
{
if (scan_buttonState == HIGH)
{
scan_counter++;
delay(50); // Delay a little bit to avoid bouncing
}
}
scan_lastButtonState = scan_buttonState;
if (scan_counter % 2 == 1 && LEDstate <= array_HEX[idx]) {
pins_state();
for (int x = 0; x <= idx; x++) {
digitalWrite(array_PINS[x], bitRead(LEDstate, x));
}
} else {
LEDstate = 0x1;
scan_counter = 0;
digitalWrite(13, HIGH);
}
}
//---------------------------------------------------------------------------
// Function to turn ON/OFF the LEDs/ Relays
void pins_state()
{
currentMillis = millis();
if ((currentMillis - previousMillis) >= scan_interval) {
previousMillis = currentMillis;
// Use "<<" to "bit-shift" everything to the left once
LEDstate = LEDstate << 1;
}
}
Any help it's much appreciated! FWIW, I use an Arduino diecimila (I think!).
Also, as you can probably tell I'm far from being a programming wizard so any general suggestions/ improvements are more than welcome!