Go Down

Topic: Arduino Class not working (Read 99 times) previous topic - next topic

nischalm

Hi Here is the code I'm trying to implement to use classes in Arduino.
it has the main code and a header file that I created.

here is the main code
Code: [Select]

#include "RelayBoard.h"
RelayBoard relay;

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
relay.relay_loop();
}

and here is the header file that I created.

Code: [Select]

#ifndef _RELAY_BOARD_H
#define _RELAY_BOARD_H


#include "Arduino.h"
#include <Wire.h>
#include <Adafruit_MCP23017.h>


Adafruit_MCP23017 mcp;

class RelayBoard {

  public:
    RelayBoard()
    {
      mcp.begin(0);      // Default device address 0
      mcp.pinMode(0, OUTPUT);
      mcp.pinMode(1, OUTPUT);
      mcp.pinMode(2, OUTPUT);
      mcp.pinMode(3, OUTPUT);
      mcp.pinMode(4, OUTPUT);
      mcp.pinMode(5, OUTPUT);
      mcp.pinMode(6, OUTPUT);
      mcp.pinMode(7, OUTPUT);
      mcp.pinMode(8, OUTPUT);
      mcp.pinMode(9, OUTPUT);
      mcp.pinMode(10, OUTPUT);
      mcp.pinMode(11, OUTPUT);
      mcp.pinMode(12, OUTPUT);
      mcp.pinMode(13, OUTPUT);
      mcp.pinMode(14, OUTPUT);
      mcp.pinMode(15, OUTPUT);
    }
   
    void relay_loop()
    {
      int _i;
      for ( _i = 0; _i < 16; _i++)
      {
        On(_i);
        delay(250);
        Off(_i);
        delay(250);
      }
    }

    void On(int x)
    {
      mcp.digitalWrite(x, HIGH);
    }
    void Off(int x)
    {
      mcp.digitalWrite(x, LOW);
    }

};

#endif // _RELAY_BOARD_H


Both files are in the same folder.
Arduino compiles and uploads the code successfully but nothing seems to happen in the and the pin doesn't output anything.
I have followed instructions from this https://www.youtube.com/watch?v=1phVBggIVoU&t=573s for using class.

larryd

#1
May 23, 2020, 08:16 am Last Edit: May 23, 2020, 09:10 am by larryd
Lets see your schematic.

Code: [Select]

#include "RelayBoard.h"
RelayBoard relay;

void setup()
{
  relay.begin(0);
  // put your setup code here, to run once:
}

void loop()
{
  // put your main code here, to run repeatedly:
  relay.relay_loop();
}


Code: [Select]

#ifndef _RELAY_BOARD_H
#define _RELAY_BOARD_H

#include "Arduino.h"
#include <Wire.h>
#include <Adafruit_MCP23017.h>

Adafruit_MCP23017 mcp;

class RelayBoard
{
  public:
    RelayBoard()
    {
    }

    void begin (const byte address)
    {
      mcp.begin(address);     

      mcp.pinMode(0, OUTPUT);
      mcp.pinMode(1, OUTPUT);
      mcp.pinMode(2, OUTPUT);
      mcp.pinMode(3, OUTPUT);
      mcp.pinMode(4, OUTPUT);
      mcp.pinMode(5, OUTPUT);
      mcp.pinMode(6, OUTPUT);
      mcp.pinMode(7, OUTPUT);
      mcp.pinMode(8, OUTPUT);
      mcp.pinMode(9, OUTPUT);
      mcp.pinMode(10, OUTPUT);
      mcp.pinMode(11, OUTPUT);
      mcp.pinMode(12, OUTPUT);
      mcp.pinMode(13, OUTPUT);
      mcp.pinMode(14, OUTPUT);
      mcp.pinMode(15, OUTPUT);
    }

    void relay_loop()
    {
      int _i;
      for ( _i = 0; _i < 16; _i++)
      {
        On(_i);
        delay(250);
        Off(_i);
        delay(250);
      }
    }

    void On(int x)
    {
      mcp.digitalWrite(x, HIGH);
    }
    void Off(int x)
    {
      mcp.digitalWrite(x, LOW);
    }

};

#endif // _RELAY_BOARD_H





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

6v6gt

It look like @larryd already suspects that you have code in the constructor which which may be rendered invalid when the arduino initialisation routine starts running.

nischalm

#3
May 23, 2020, 06:53 pm Last Edit: May 23, 2020, 06:54 pm by nischalm
HI ,

Thanks for such quick replies.
I have created a relay board using MCP23017 which uses i2c for communication.I have Attached schematic and board pic.

I'm just trying to connect Uno to this Relay board and get the relays working.

If I don't create a separate class and just put the code directly like below its working fine.
Code: [Select]



#include <Wire.h>
#include <Adafruit_MCP23017.h>


Adafruit_MCP23017 mcp;
#define MCP_B0 0
#define MCP_B1 1
#define MCP_B2 2
#define MCP_B3 3
#define MCP_B4 4
#define MCP_B5 5
#define MCP_B6 6
#define MCP_B7 7
#define MCP_A0 8
#define MCP_A1 9
#define MCP_A2 10
#define MCP_A3 11
#define MCP_A4 12
#define MCP_A5 13
#define MCP_A6 14
#define MCP_A7 15

int i;

void setup() {
  Serial.begin(9600);
  Serial.println("Entered Setup");
  mcp.begin();      // Default device address 0

  mcp.pinMode(MCP_A0, OUTPUT);
  mcp.pinMode(MCP_A1, OUTPUT);
  mcp.pinMode(MCP_A2, OUTPUT);
  mcp.pinMode(MCP_A3, OUTPUT);
  mcp.pinMode(MCP_A4, OUTPUT);
  mcp.pinMode(MCP_A5, OUTPUT);
  mcp.pinMode(MCP_A6, OUTPUT);
  mcp.pinMode(MCP_A7, OUTPUT);
  mcp.pinMode(MCP_B0, OUTPUT);
  mcp.pinMode(MCP_B1, OUTPUT);
  mcp.pinMode(MCP_B2, OUTPUT);
  mcp.pinMode(MCP_B3, OUTPUT);
  mcp.pinMode(MCP_B4, OUTPUT);
  mcp.pinMode(MCP_B5, OUTPUT);
  mcp.pinMode(MCP_B6, OUTPUT);
  mcp.pinMode(MCP_B7, OUTPUT);

  Serial.println("Exited setup");
}
void loop() {
  Serial.println("Entered void loop");
  for ( i = 0; i < 16; i++)
  {
    Serial.println(i);
    mcp.digitalWrite(i, HIGH);
    delay(100);
    mcp.digitalWrite(i, LOW);
    delay(100);
  }

}


Thanks for all the help guys.. Really appreciate it. :)

larryd

#4
May 23, 2020, 07:06 pm Last Edit: May 23, 2020, 07:08 pm by larryd
OP's image:



Did you try the changes shown in post #1 ?

This image is not viewable, too pixelated.





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

nischalm

Hi Guys,

I tried what larryd posted.
Added a begin function in void setup and it worked!.

Thank you all for the help :D.

larryd

It's always best to review responder's posts to see if there is a solution waiting for you ;)



Good programming.





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Go Up