MCP23017 blink without delay

Hi everyone,

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);
    }
  }
}

Hello,

Change to else if (state == 1), or state will always be set to 0.

A better idea is to change the type of state to bool, then you can invert its value by doing state = !state;, so that you don't need the if/else if

1 Like

Thanks guix, the LED at GPIO 0 is blinking now. Now, working to blink all the 16 LEDs.

#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;
      }
      else if (state == 1)
      {
        state = 0;
      }
      mcp.digitalWrite (i, state);
    }
  }
}

Guys, I figured it out. Thanks.

#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;
int i, state = 0;
unsigned long current_time, previous_time = 0;

void setup()
{
  mcp.begin();
  for (i = 0; i < 16; i++)
  {
    mcp.pinMode(i, OUTPUT);
  }
}

void loop()
{
  for (i = 0; i<16; i++)
  {
    state = 0;
    do
    {
      current_time = millis();
      if (current_time - previous_time >= 500)
      {
        previous_time = current_time;
        if (state == 0)
        {
          state = 1;
          mcp.digitalWrite (i, state);
        }
        else if (state == 1)
        {
          state = 0;
          mcp.digitalWrite (i, state);
          state = 2;
        }
      }
    }
    while (state != 2);
  }
}

I didn't have to look far.

#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.
}

why int i starts from 0 to 16, not 0 to 15 for MCP23017?

Where you get 16? I'm already wearing glasses.

for (i = 0; i < 16; i++)

I tried to change to for (i = 0; i < 15; i++), the 16th LED will not light up.

should be <= 15 if you want [15] to process.

I didn't correct the comment which is in the original I worked from.
I left this comment below;

  //  here you need to decide when to increment  i  including 0 to 15 bounds keeping.

It would be an if ( ++i > 15 ) i = 0; kind of line.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.