Eleven with Realy8 sheild, button input led&relay output

The "Eleven" is our Uno-equivalent Arduino-compatible board, but with a number of improvements including prototyping area, a mini-USB connector, LEDs mounted near the edge, and the D13 LED isolated using a FET. [Product page]
ChrysP
Posts: 3
Joined: Sun Dec 29, 2013 12:00 pm

Eleven with Realy8 sheild, button input led&relay output

Post by ChrysP » Sun Dec 29, 2013 12:29 pm

Evening All,
Happy Holidays and thanks for reading my essay :)

I have recently purchased the Eleven board with the Relay8 shield.

I have been through the projects booklet and am enjoying the steep learning curve. I took some of the basic projects, added buttons/extra LEDs and added lines to the sketch to make it all work, good times.

The project/functionality I am working on now is a button input, which switches an LED output and a relay channel on the relay 8 shield.

I have meshed the programming from the led, button and the relay examples.

I can get the LED and relay to activate via the serial monitor inputs on the keyboard, however when I try to add the next step of the button inputs, nothing happens.

I replaced "if( command == '1' )" with "if (buttonState == HIGH)" for the relay 1 control.

Code: Select all


#include "Wire.h"

#define I2C_ADDR  0x20  // 0x20 is the address with all jumpers removed
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status



void setup()
{
  pinMode(ledPin, OUTPUT);  
  pinMode(buttonPin, INPUT); 
  Serial.begin( 38400 );
  Serial.println("RELAY8 demonstration starting up");

  Wire.begin(); // Wake up I2C bus

  // Set I/O bank A to outputs
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // Set all of bank A to outputs
  Wire.endTransmission();
  
  Serial.println("Ready. Type 0 to turn off relays, 1 - 8 to activate a relay.");
}

void loop()
{
   buttonState = digitalRead(buttonPin);
  int command = 0;   
    
   if (Serial.available()) {
    command = Serial.read();
    if( command == '0' )
    {
      sendValueToLatch(0);
      digitalWrite(ledPin, LOW);
      Serial.println("Resetting all relays");
    }
  if (buttonState == HIGH) 
  {
      sendValueToLatch(1);
      digitalWrite(ledPin, HIGH);
      Serial.println("Activating relay 1");
    }
    if( command == '2' )
    {
      sendValueToLatch(2);
      Serial.println("Activating relay 2");
    }
    
  }
}

void sendValueToLatch(int latchValue)
{
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x12);        // Select GPIOA
  Wire.write(latchValue);  // Send value to bank A
  Wire.endTransmission();
}
Nothing seems to happen when I push/hold the button, but when I enter 2 on the keyboard, relay 2 still closes.

If anyone can point me in the right direction that would be greatly appreciated.

Regards,
Chrys
Last edited by ChrysP on Sun Dec 29, 2013 11:31 pm, edited 1 time in total.

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Eleven with Realy8 sheild, button input led&relay output

Post by andrew » Sun Dec 29, 2013 9:46 pm

No need to assign the state of the button to a variable if you're just checking it.
Remove

Code: Select all

buttonState = digitalRead(buttonPin);
and just check the button when necessary with

Code: Select all

if (digitalRead(buttonPin)==HIGH)...
If you're learning you should check out our new book "Arduino Workshop":
http://www.freetronics.com/collections/ ... o-workshop

ChrysP
Posts: 3
Joined: Sun Dec 29, 2013 12:00 pm

Re: Eleven with Realy8 sheild, button input led&relay output

Post by ChrysP » Sun Dec 29, 2013 11:45 pm

Morning John,

Thanks for the prompt reply. I'm looking at the Arduino workshop book, just need to get it approved by the Minister of War and Finance.

I removed the "buttonState" line and changed the "if" line as per your post, still nothing happens when I push the button. Entering "2" on the keyboard still activates relay 2.

I have reloaded the "button" example to confirm that yes my button is actually working.

I also added a delay, just to see if it was happening, maybe it was too fast or I wasn't seeing it.

Silly question, can the eleven board accept both serial monitor inputs and a button input in the same sketch? I imagine it should if you can have multiple inputs, why not multiple source inputs.

below is the updated code

Code: Select all


#include "Wire.h"

#define I2C_ADDR  0x20  // 0x20 is the address with all jumpers removed
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status



void setup()
{
  pinMode(ledPin, OUTPUT);  
  pinMode(buttonPin, INPUT); 
  Serial.begin( 38400 );
  Serial.println("RELAY8 demonstration starting up");

  Wire.begin(); // Wake up I2C bus

  // Set I/O bank A to outputs
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // Set all of bank A to outputs
  Wire.endTransmission();
  
  Serial.println("Ready. Type 0 to turn off relays, 1 - 8 to activate a relay.");
}

void loop()
{
   int command = 0;   
    
   if (Serial.available()) {
    command = Serial.read();
    if( command == '0' )
    {
      sendValueToLatch(0);
      digitalWrite(ledPin, LOW);
      Serial.println("Resetting all relays");
    }
if (digitalRead(buttonPin)==HIGH)
  {
      sendValueToLatch(1);
      digitalWrite(ledPin, HIGH);
      Serial.println("Activating relay 1");
      delay(5000);
    }
    if( command == '2' )
    {
      sendValueToLatch(2);
      Serial.println("Activating relay 2");
    }
    
  }
}

void sendValueToLatch(int latchValue)
{
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x12);        // Select GPIOA
  Wire.write(latchValue);  // Send value to bank A
  Wire.endTransmission();
}
Thanks again John, much appreciated

Chrys

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Eleven with Realy8 sheild, button input led&relay output

Post by andrew » Mon Dec 30, 2013 11:18 am

Hello
Sorry for the delay in my reply, drove from Toowoomba to Newcastle today. Phew.
Can you give the following code a try? You may need to hold the button down a little as there's code running (i.e. looking at serial) when the Arduino isn't looking at the button's digital input pin:

Code: Select all


#include "Wire.h"

#define I2C_ADDR  0x20  // 0x20 is the address with all jumpers removed
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status



void setup()
{
  pinMode(ledPin, OUTPUT);  
  pinMode(buttonPin, INPUT); 
  Serial.begin( 38400 );
  Serial.println("RELAY8 demonstration starting up");

  Wire.begin(); // Wake up I2C bus

  // Set I/O bank A to outputs
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // Set all of bank A to outputs
  Wire.endTransmission();

  Serial.println("Ready. Type 0 to turn off relays, 1 - 8 to activate a relay.");
}

void loop()
{
  int command = 0;   

    if (digitalRead(buttonPin)==HIGH)
    {
      sendValueToLatch(1);
      digitalWrite(ledPin, HIGH);
      Serial.println("Activating relay 1");
      delay(5000);
    }

  if (Serial.available()) 
  {
    command = Serial.read();
    if( command == '0' )
    {
      sendValueToLatch(0);
      digitalWrite(ledPin, LOW);
      Serial.println("Resetting all relays");
    }

    if( command == '2' )
    {
      sendValueToLatch(2);
      Serial.println("Activating relay 2");
    }

  }
}

void sendValueToLatch(int latchValue)
{
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x12);        // Select GPIOA
  Wire.write(latchValue);  // Send value to bank A
  Wire.endTransmission();
}

And you can use the code "DISCOUNT10" during the checkout process for 10% off.

ChrysP
Posts: 3
Joined: Sun Dec 29, 2013 12:00 pm

Re: Eleven with Realy8 sheild, button input led&relay output

Post by ChrysP » Mon Dec 30, 2013 11:46 am

Thanks John,

Hope you enjoyed the drive, sounds like you need one of those GPS speed alert trackers so you don't speed. I know this guy that sells kits with a GPS shield, might be useful.....

Thanks for the code update, it's working now, it seems like the "serial commands" were in the wrong order. Now that I have the code sorted, I can add more buttons and activate more relays.

I'll be sure to send you a pic of the finished project.

Thanks very much for your assistance and your time over the holidays.

Enjoy New Castle.

Regards

Chrys

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Eleven with Realy8 sheild, button input led&relay output

Post by andrew » Mon Dec 30, 2013 10:52 pm

ChrysP wrote:Thanks John,

Hope you enjoyed the drive, sounds like you need one of those GPS speed alert trackers so you don't speed. I know this guy that sells kits with a GPS shield, might be useful.....

Thanks for the code update, it's working now, it seems like the "serial commands" were in the wrong order. Now that I have the code sorted, I can add more buttons and activate more relays.

I'll be sure to send you a pic of the finished project.

Thanks very much for your assistance and your time over the holidays.

Enjoy New Castle.

Regards

Chrys
Glad it's working. Let me know if you have any more questions.
We swapped drivers every 200km so wasn't that bad. I have a home-made GPS alert device which I'll document soon for others to see.

Sailplane
Posts: 3
Joined: Sat Aug 16, 2014 7:57 am

Re: Eleven with Realy8 sheild, button input led&relay output

Post by Sailplane » Sun Aug 17, 2014 5:14 am

I hope that someone can help me I have a freetronics eleven with a Relay 8 sheild
I have been trying for days and are out of my depth here.
What I am trying to do is have 8 push buttons that drive 8 pins high/low then the 8 pins drive the relays on and off.
I need all the relays to operate independently so operating say pin 3 will not change the state of any other pin.

Help Scott

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Eleven with Realy8 sheild, button input led&relay output

Post by angusgr » Mon Aug 18, 2014 7:05 am

Hi Scott,

Can you post any code you have so far, so we can take a look at it?

Cheers,


Angus

Sailplane
Posts: 3
Joined: Sat Aug 16, 2014 7:57 am

Re: Eleven with Realy8 sheild, button input led&relay output

Post by Sailplane » Tue Aug 19, 2014 4:39 am

Hi Angus
Thanks for getting back to me. My real goal is to get a controller to operate an engine extend retracted system for my sailplane. I am installing 3 jets into a ASH25 glider you can see one on youTube. This programming side of thing unfortunately will just be outside of my skill sets. I have purchase a 4d touch screen (LCD-43-PT) and would love to have this communicate with the 3 engine control units so that I can start and control the engines meanwhile view the temps and other parameters from the cockpit. I think a mega can do all that I need. At this time I need to control 7 relays, 4 PWM servos, Monitor 5 Batteries, 7 limit switches, 3 switches, 3 sliders.
The code has to sequence the opening of the engine doors raise the engine pylon close doors apply mains power to engines systems communicate with 3 engine control units and one touch screen,. Then sequence the reverse to lower the engine pylon back into the glider.
The youtube video of the danger shield on the spark fun sight is very close to what I need to control some of the engine parameters and LCD touch screen.
Maybe you can help me get somewhere with this project I somehow are starting to realise that just how big a deal the controller programming is for me, my default position is to use relays, switches, leds and no LCD

Code that I have to control the Freetronices 8 channel relay
/*
* Example sketch to control the RELAY8 8-Channel Relay
* Driver Shield. Open serial console at 38400bps, and
* send value "0" to reset all relays, or a channel number
* to activate that relay. Requires Arduino IDE v1.0.1
* or newer.
*/

#include "Wire.h"

#define I2C_ADDR 0x20 // 0x20 is the address with all jumpers removed

void setup()
{
Serial.begin( 38400 );
Serial.println("RELAY8 demonstration starting up");

Wire.begin(); // Wake up I2C bus

// Set I/O bank A to outputs
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // Set all of bank A to outputs
Wire.endTransmission();

Serial.println("Ready. Type 0 to turn off relays, 1 - 8 to activate a relay.");
}

void loop()
{
int command = 0;
if (Serial.available()) {
command = Serial.read();
if( command == '0' )
{
sendValueToLatch(0);
Serial.println("Resetting all relays");
}
if( command == '1' )
{
sendValueToLatch(1);
Serial.println("Activating relay 1");
}
if( command == '2' )
{
sendValueToLatch(2);
Serial.println("Activating relay 2");
}
if( command == '3' )
{
sendValueToLatch(4);
Serial.println("Activating relay 3");
}
if( command == '4' )
{
sendValueToLatch(8);
Serial.println("Activating relay 4");
}
if( command == '5' )
{
sendValueToLatch(16);
Serial.println("Activating relay 5");
}
if( command == '6' )
{
sendValueToLatch(32);
Serial.println("Activating relay 6");
}
if( command == '7' )
{
sendValueToLatch(64);
Serial.println("Activating relay 7");
}
if( command == '8' )
{
sendValueToLatch(128);
Serial.println("Activating relay 8");
}
}
}

void sendValueToLatch(int latchValue)
{
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select GPIOA
Wire.write(latchValue); // Send value to bank A
Wire.endTransmission();
}

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Eleven with Realy8 sheild, button input led&relay output

Post by angusgr » Tue Aug 19, 2014 5:24 am

Hi Sailplane,

It sounds like a pretty ambitious but fun project!

Where in the world are you located? There might be a hobby group or a hackerspace nearby where you could look for a potential collaborator for your project? We can help with simple things like fixing the above sketch, but to progress further you probably want to find someone who can help you out more directly (ideally another sailplane enthusaist, or someone who is interested in learning more about those things).

Regards,


Angus

Post Reply