I am working on blinking one LED at a time for 16 LEDs on an MCP23017 16-bit io expander. I am using an Arduino Mega. I have a problem, the code below using blink without delay could not light a single LED. Please help.
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;
int i, state = 0;
unsigned long current_time, previous_time = 0;
void setup()
{
Serial.begin(19200);
mcp.begin();
for (i = 0; i < 15; i++)
{
mcp.pinMode(i, OUTPUT);
}
}
void loop()
{
i = 0;
//for (i = 0; i < 15; i++)
{
current_time = millis();
if (current_time - previous_time >= 2000)
{
previous_time = current_time;
if (state == 0)
{
state = 1;
}
if (state == 1)
{
state = 0;
}
mcp.digitalWrite (i, state);
}
}
}
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;
int i, state = 0;
unsigned long current_time, previous_time = 0;
void setup()
{
Serial.begin(19200); // why so slow?
mcp.begin();
for (i = 0; i < 15; i++)
{
mcp.pinMode(i, OUTPUT);
}
}
void loop()
{
// move this line up
i = 0; // the initializer belongs in setup KUDOS for using void loop() to loop!
//for (i = 0; i < 15; i++)
{
current_time = millis();
if (current_time - previous_time >= 2000)
{
previous_time = current_time;
if (state == 0)
{
state = 1;
}
else
{
state = 0;
}
mcp.digitalWrite (i, state);
}
}
// here you need to decide when to increment i including 0 to 15 bounds keeping.
}