Hello everyone.
I am new to the Arduino world, and have recently purchased a Sparkfun 4x4 LED keypad, controlled by an Arduino Mega 2560.
I would like to code it so that the LEDs randomly blink off, then back on again. Not one at a time, but have them completely random (so maybe as three to five or so blink off at the same time sometimes).
The code that I have to light them up is off of the Sparkfun tutorial page:
/******************************************************************************
red-only.ino
Byron Jacquot @ SparkFun Electronics
1/6/2015
Example to drive the red LEDs in the RGB button pad.
Exercise 1 in a series of 3.
https://learn.sparkfun.com/tutorials/bu … hrome-leds
Development environment specifics:
Developed in Arduino 1.6.5
For an Arduino Mega 2560
This code is released under the MIT License.
Distributed as-is; no warranty is given.
******************************************************************************/
//config variables
#define NUM_LED_COLUMNS (4)
#define NUM_LED_ROWS (4)
#define NUM_COLORS (1)
// Global variables
static bool LED_buffer[NUM_LED_COLUMNS][NUM_LED_ROWS];
static int32_t next_advance;
static uint8_t led_index;
static const uint8_t ledcolumnpins[NUM_LED_COLUMNS] = {42,43,44,45};
static const uint8_t colorpins[NUM_LED_ROWS] = {22,30,33,36};
static void setuppins()
{
uint8_t i;
// initialize all of the output pins
// LED column lines
for(i = 0; i < NUM_LED_COLUMNS; i++)
{
pinMode(ledcolumnpins*, OUTPUT);*
// with nothing selected by default
digitalWrite(ledcolumnpins, HIGH);
}
// LED row lines
for(i = 0; i < NUM_LED_ROWS; i++)
{
pinMode(colorpins, OUTPUT);
// with nothing driven by default
digitalWrite(colorpins, LOW);
}
}
static void scan()
{
static uint8_t current = 0;
uint8_t val;
uint8_t i, j;
// Select a column
digitalWrite(ledcolumnpins[current], LOW);
// write the row pins
for(i = 0; i < NUM_LED_ROWS; i++)
{
if(LED_buffer[current])
{
digitalWrite(colorpins, HIGH);
}
}
delay(1);
digitalWrite(ledcolumnpins[current], HIGH);
for(i = 0; i < NUM_LED_ROWS; i++)
{
digitalWrite(colorpins, LOW);
}
// Move on to the next column
current++;
if (current >= NUM_LED_COLUMNS)
{
current = 0;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print(“Starting Setup…”);
// setup hardware
setuppins();
// init global variables
next_advance = millis() + 1000;
led_index = 0;
// Initialize the LED display array
for(uint8_t i = 0; i < NUM_LED_COLUMNS; i++)
{
for(uint8_t j = 0; j < NUM_LED_ROWS; j++)
{
LED_buffer[j] = false;
}
}
// Set the first LED in the buffer on
LED_buffer[0][0] = true;
Serial.println(“Setup Complete.”);
}
void loop()
{
// put your main code here, to run repeatedly:
scan();
if(millis() >= next_advance)
{
next_advance = millis()+1000;
LED_buffer[led_index/NUM_LED_COLUMNS][led_index%NUM_LED_COLUMNS] = false;
led_index++;
led_index %= (NUM_LED_COLUMNS * NUM_LED_ROWS);
LED_buffer[led_index/NUM_LED_COLUMNS][led_index%NUM_LED_COLUMNS] = true;
}
}
Any and all help would be much appreciated.
P.C.