Hi!
I have 7 Relais which should be switched bitwise. Im using this code
const byte volnumPinsright = 7;
byte volpinsright[] = {
0, 1, 2, 3, 4, 5, 6};
void setvol(int vol){
for (byte i=0; i<volnumPinsright; i++) {
byte state = bitRead(vol, i);
shifter.setPin(volpinsright[i],state);
}
shifter.write();
}
Im using a 74HC595 Shiftregister with this Library
http://bildr.org/2011/08/74hc595-breakout-arduino/
Do you see any logical mistakes in this piece of code? The problem is that it doesnt work properly, the values dont agree with the online conversion tools ( like this http://easycalculation.com/decimal-converter.php)
Any suggestions?
EDIT:
Here the whole code
#include <Wire.h>
#define myAdress 0x46 //I2C Adresse dieses Moduls
boolean debug = true;
//Konfigurieren der Shift register
#include <Shifter.h>
#define SER_Pin 11 //SER_IN
#define RCLK_Pin 8 //L_CLOCK
#define SRCLK_Pin 12 //CLOCK
#define NUM_REGISTERS 2
Shifter shifter(SER_Pin, RCLK_Pin, SRCLK_Pin, NUM_REGISTERS);
//Lautstärkeregelung
//Rechts
const byte volnumPinsright = 7; //Anzahl der Pins für die Lautstärkeregelung
byte volpinsright[] = {
0, 1, 2, 3, 4, 5, 6}; //Pins für die Lautstärkeregelung (7 Stück)
//Links
const byte volnumPinsleft = 7; //Anzahl der Pins für die Lautstärkeregelung
byte volpinsleft[] = {
8, 9, 10, 11, 12, 13, 14}; //Pins für die Lautstärkeregelung (7 Stück)
//Quellenwahl
int currentout = 0; //aktuelle Ausgang
void setup()
{
Wire.begin(myAdress); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
if (debug){ Serial.begin(9600);} // start serial for output
}
void loop()
{
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
int data[howMany];
while( Wire.available()) // loop through all
{
//dfuliod
for (int i=0; i < howMany;i++){
data[i] = Wire.read(); // receive byte as int
}
}
work(data);
}
void work(int data[]){
switch (data[0]){
case 1:
{ //Lautstärke einstellen
for (byte i=0; i<volnumPinsright; i++) {
byte state = bitRead(data[1], i);
shifter.setPin(volpinsright[i],state);
}
shifter.write();
}
break;
case 2:
{ //Lautstärke einstellen
for (byte i=0; i<volnumPinsleft; i++) {
byte state = bitRead(data[1], i);
shifter.setPin(volpinsleft[i],state);
}
shifter.write();
}
break;
case 3:
{//Eingang wählen
int output = (data[1]+1); //unsere Eingänge fangen bei 2 an.
digitalWrite(currentout,LOW);
delay(5); //Warten bis sich das Relais geschaltet hat.
digitalWrite(output, HIGH);
currentout = output;
}
break;
case 4:
{//Ausgang wählen
if (data[1]==1){//Entzerrer aktivieren
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
else{//Entzerrer deaktivieren
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
break;
}
}
}
Receiving via I2C and switching the relays