Project

General

Profile

Resolved: Using Output plug

Added by hatfieldr over 5 years ago

Hi I’m new to Jee Labs & a novice programmer. I’m trying to use an output plug on an arduino clone (from openenergymonitor) which has 4 jeenodes to create 8 independent outputs on node3

the following code successfully blinks pin 2:

#include 

PortI2C myport (3);
DeviceI2C expander (myport, 0x26); // also works with output plug if 0x26/0x27

enum {
  MCP_IODIR, MCP_IPOL, MCP_GPINTEN, MCP_DEFVAL, MCP_INTCON, MCP_IOCON,
  MCP_GPPU, MCP_INTF, MCP_INTCAP, MCP_GPIO, MCP_OLAT
};

static void exp_setup () {
    expander.send();
    expander.write(MCP_IODIR);
    expander.write(0); // all outputs
    expander.stop();
}

static void exp_write (byte value) {
    expander.send();
    expander.write(MCP_GPIO);
    expander.write(value);
    expander.stop();
}


void setup() {
    exp_setup();
}

void loop() {   
    // blinking light

        exp_write(1 << 2);
            delay (500);
            exp_write(0 << 2);
            delay (500);
}

This seems to work for one pin but not if expanded to multiple pins independently
I clearly have the wrong syntax for exp_write
Please could someone tell me the correct syntax to turn each pin on & off independently?
Sorry for such a basic question but I’m stuck

Richard


Replies (4)

RE: Using Output plug - Added by martynj over 5 years ago

@hatfielr,

Does this loop blink all outputs?

void loop() {   
    // blink all pins by alternately turning on odd and even pin numbers

        exp_write(0xAA);
            delay (500);
            exp_write(0x55);
            delay (500);
}

RE: Using Output plug - Added by hatfieldr over 5 years ago

yes but I am trying to control each output independently
I therefore wish to find the code to turn each output on & off separately (& leave them on or off until the opposite command is received)
I have 8 relays to control!

any ideas?

RE: Using Output plug - Added by jcw over 5 years ago

The issue you’re running into is that all bit settings must be sent as a single value.
The solution is to use a variable and set/clear individual bits, and then send that entire value each time you make a change:

uint8_t allBits = 0;
...
bitSet(allBits, 3);
exp_write(allBits);
...
bitClear(allBits, 3);
exp_write(allBits);
...
bitSet(allBits, 5);
exp_write(allBits);
...
bitClear(allBits, 5);
exp_write(allBits);

(bitSet and bitClear are convenience macro’s defined in the Arduino/Wiring headers)

-jcw

RE: Using Output plug - Added by hatfieldr over 5 years ago

Many Thanks!
I think I now have a working sketch to operate 8 relays independently using the Output Plug!

I looked everywhere t try and find how to control the outputs of the Output Plug independently to no avail
I think it would be really useful to add a simple example sketch to show how to do this!

Regards

Richard

    (1-4/4)