8 Channel relay operation

Got comments, questions, or tips about any of the shields listed on ShieldList.org? Post them here!
Post Reply
chupps
Posts: 7
Joined: Wed Feb 26, 2014 2:50 am

8 Channel relay operation

Post by chupps » Wed Feb 26, 2014 5:07 am

Hi,
I am having trouble writing some code to turn off singe channels on the 8 channel relay shield without disturbing other channels. In the quick start guide it mentions using "bitwise operators" I have tried using bitwise not ~ to turn off the desired channel but it simultaneously turns on all other channels.
Any ideas?

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

Re: 8 Channel relay operation

Post by andrew » Wed Feb 26, 2014 10:15 pm

Hello

NOT will invert all the bits in a byte. For example:

byte a = B00111100;
byte b = 0;
b = ~a;

now b contains 11000011.

Moving on. Let's say you have the first four relays on, by previously sending
B00001111
and you want to turn on relay 7 and 8. You can use OR(|) for example
B00001111 | B11000000 = B11001111

then send the final value.
More information > http://arduino.cc/en/Reference/BitwiseAnd

chupps
Posts: 7
Joined: Wed Feb 26, 2014 2:50 am

Re: 8 Channel relay operation

Post by chupps » Mon Mar 03, 2014 8:30 am

Thanks for the reply, the link really helped.
Is it possible to do the same with decimal decimal?

chupps
Posts: 7
Joined: Wed Feb 26, 2014 2:50 am

Re: 8 Channel relay operation

Post by chupps » Mon Mar 03, 2014 9:24 am

Also, I understand the principle but what if you do not know if the original 4 relays have been switched off by another function already?

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

Re: 8 Channel relay operation

Post by andrew » Mon Mar 03, 2014 9:30 pm

chupps wrote:Also, I understand the principle but what if you do not know if the original 4 relays have been switched off by another function already?
You might want to store the last byte sent to the shield in another variable, or even the internal EEPROM. Then this can be polled when required.
http://arduino.cc/en/Reference/EEPROM
http://tronixstuff.com/2011/03/16/tutor ... lt-eeprom/

chupps
Posts: 7
Joined: Wed Feb 26, 2014 2:50 am

Re: 8 Channel relay operation

Post by chupps » Wed Mar 05, 2014 10:41 am

Thanks for all the help John, I am still a real novice at this.

This is part of the code where I need to use relays, its a simple thermostat. Am I on the right track? can't get it to verify


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

void fermentControl ()
{
int newLatchValue = B00000000;
sensors.requestTemperatures();
int temp0 = (sensors.getTempCByIndex(0));
Serial.print("Fermentation Temperature: ");
Serial.println(temp0);


if( temp0 > fermentTemp )
{
newLatchValue | B00000001 = newLatchValue;

sendValueToLatch(newLatchValue);
}
else if (temp0 < fermentTemp )
{
newLatchValue | B00000000 = newLatchValue;

sendValueToLatch(newLatchValue);
}

}

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

Re: 8 Channel relay operation

Post by andrew » Wed Mar 05, 2014 9:15 pm

chupps wrote:Thanks for all the help John, I am still a real novice at this.

This is part of the code where I need to use relays, its a simple thermostat. Am I on the right track? can't get it to verify

Code: Select all


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

void fermentControl ()
{
  int newLatchValue = B00000000;  
  sensors.requestTemperatures();
  int temp0 = (sensors.getTempCByIndex(0));
  Serial.print("Fermentation Temperature: "); 
  Serial.println(temp0);
  

  if( temp0 > fermentTemp )
  {
    newLatchValue | B00000001 = newLatchValue;
    
    sendValueToLatch(newLatchValue);
  }
  else if (temp0 < fermentTemp )
  {
    newLatchValue | B00000000 = newLatchValue;

    sendValueToLatch(newLatchValue);
  }

}
Could you please post the entire sketch? Or if you want to keep it private email it to support@freetronics.com

chupps
Posts: 7
Joined: Wed Feb 26, 2014 2:50 am

Re: 8 Channel relay operation

Post by chupps » Thu Mar 06, 2014 10:02 am

Code: Select all

// include the library codes:
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define  lcdBacklightPin    10
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#include "Wire.h"

// (DallasTemperature) Data wire is plugged into pin 10 on the Arduino
#define ONE_WIRE_BUS 12
// (DallasTemperature) Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// (DallasTemperature) Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// (8 channel relay) 0x20 is the address with all jumpers removed
#define I2C_ADDR  0x20 


//Fermentation control
int fermentTemp = 18;
int fermentTempOne = 18;
int brightBeerTemp = 18;

int newLatchValue = B00000000;

int temp0;
int temp1;
int temp2;

//Buttons
int y;
int state = 0;
int right=0,left=0,up=0,down=0;
int sel=0;
int last_st=0,st=0;



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



void fermentControl ()
{
  sensors.requestTemperatures();
  temp0 = (sensors.getTempCByIndex(0));
  Serial.print("Fermentation Temperature: "); 
  Serial.println(temp0);


  if( temp0 > fermentTemp )
  {
    newLatchValue | B00000001 = newLatchValue;
    
    sendValueToLatch(newLatchValue);
  }
  else if (temp0 < fermentTemp )
  {
    newLatchValue | B00000000 = newlatchValue;
    
    sendValueToLatch(newLatchValue);
  }

}



void fermentControlOne ()
{
  sensors.requestTemperatures();
  int temp1 = (sensors.getTempCByIndex(1));
  Serial.print("Fermentation Temperature: "); 
  Serial.println(temp0);


  if( temp1 > fermentTempOne )
  {
    sendValueToLatch(128);
  }
  else if (temp1 < fermentTempOne )
  {
    sendValueToLatch(0);
  }

}



void brightBeerControl ()
{
  sensors.requestTemperatures();
  int temp2 = (sensors.getTempCByIndex(2));
  Serial.print("Fermentation Temperature: "); 
  Serial.println(temp0);


  if( temp2 > brightBeerTemp )
  {
    sendValueToLatch(128);
  }
  else if (temp2 < brightBeerTemp )
  {
    sendValueToLatch(0);
  }



}
void process_state()
{
  switch (state) {
  case 1://right
    right = right + 1;
    break;
  case 2://up
    up = up + 1;
    break;
  case 3://down
    up = up - 1;
    break;
  case 4://left
    right = right - 1;
    break;
  case 5:
    sel = sel + 1;
  }
}



void read_state()
{
  state = 0;
  y = analogRead (0);

  if (y < 100) {
    //lcd.print ("Right ");
    state = 1;
  }
  else if (y < 200) {
    //lcd.print ("Up    ");
    state = 2;
  }
  else if (y < 400){
    //lcd.print ("Down  ");
    state = 3;
  }
  else if (y < 600){
    //lcd.print ("Left  ");
    state = 4;
  }
  else if (y < 800){
    //lcd.print ("Select");
    state = 5;
  }
}



void setup()//......................................................................... 
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode( lcdBacklightPin, INPUT );
  digitalWrite(lcdBacklightPin, LOW);

  sensors.begin();

  // Set I/O bank A to outputs
  Wire.begin();

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

  lcd.setCursor(0,0);
  lcd.print("Setup complete");
  delay(1000);

}



void loop() {//..................................................................

  last_st = state;
  read_state();
  st = state;
  if(st != last_st){
    process_state();
  }

  switch (sel) {
  case 1:
    {
      sensors.requestTemperatures();
      int temp0 = (sensors.getTempCByIndex(0));

      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Ferment Control");
      lcd.setCursor(0,1);
      lcd.print("Set/Act:");
      lcd.setCursor(8,1);
      lcd.print(fermentTemp);
      lcd.setCursor(10,1);
      lcd.print("c");
      lcd.setCursor(11,1);
      lcd.print("/");
      lcd.setCursor(12,1);
      lcd.print(temp0);
      lcd.setCursor(14,1);
      lcd.print("c");

      if(state == 2)
      {
        fermentTemp ++;
      }
      if(state == 3)
      {
        fermentTemp --;
      }
      fermentControl();
    }
    break;

  case 2:

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Ferment Control1");
    lcd.setCursor(0,1);
    lcd.print("Set Temp:");
    lcd.setCursor(9,1);
    lcd.print(fermentTempOne);
    lcd.setCursor(11,1);
    lcd.print("c");

    if(state == 2)
    {
      fermentTempOne ++;
    }
    if(state == 3)
    {
      fermentTempOne --;
    }
    fermentControlOne();

    break;

  case 3:
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Bright Beer");
    lcd.setCursor(0,1);
    lcd.print("Set Temp:");
    lcd.setCursor(9,1);
    lcd.print(brightBeerTemp);
    lcd.setCursor(11,1);
    lcd.print("c");

    if(state == 2)
    {
      brightBeerTemp ++;
    }
    if(state == 3)
    {
      brightBeerTemp --;
    }
    brightBeerControl();

    break;


  case 4:
    sel = 0;

  default:

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("F0   F1   BB ");
    lcd.setCursor(0,1);
    lcd.print(temp0);
    lcd.setCursor(2,1);
    lcd.print("c");
    lcd.setCursor(5,1);
    lcd.print(temp1);
    lcd.setCursor(7,1);
    lcd.print("c");
    lcd.setCursor(10,1);
    lcd.print(temp2);
    lcd.setCursor(12,1);
    lcd.print("c");
    fermentControl();
    fermentControlOne();
    brightBeerControl();



  }
  delay(90);
}

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

Re: 8 Channel relay operation

Post by andrew » Thu Mar 06, 2014 10:23 pm

Which temperature sensor are you using? A DS18B20?

chupps
Posts: 7
Joined: Wed Feb 26, 2014 2:50 am

Re: 8 Channel relay operation

Post by chupps » Fri Mar 07, 2014 8:37 am

Yeah the DS18B20 module, I only have one at the moment but I was going to daisy chain them.

Post Reply