1

I've been going through my program again and again, and I can't figure out what is causing it not to work as intended. First, the story: I'm trying to use an Arduino to control a bunch of relays which are controlling sockets (8 of them). I want to use an Adafruit Trellis LED panel/switches to do this. Basically, when the button is pressed, the LED lights up and the relay gets tripped. Pressing it again does the reverse.

I will provide my code for inspection.

#include <Wire.h>
#include "Adafruit_Trellis.h"

/***************************************************
This example shows reading buttons and setting/clearing buttons in a loop
"momentary" mode has the LED light up only when a button is pressed
"latching" mode lets you turn the LED on/off when pressed

Up to 8 matrices can be used but this example will show 4 or 1
****************************************************/

#define MOMENTARY 0
#define LATCHING 1
// set the mode here
#define MODE LATCHING 


Adafruit_Trellis matrix0 = Adafruit_Trellis();

// uncomment the below to add 3 more matrices
/*
Adafruit_Trellis matrix1 = Adafruit_Trellis();
Adafruit_Trellis matrix2 = Adafruit_Trellis();
Adafruit_Trellis matrix3 = Adafruit_Trellis();
// you can add another 4, up to 8
*/

// Just one
Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0);
// or use the below to select 4, up to 8 can be passed in
//Adafruit_TrellisSet trellis =  Adafruit_TrellisSet(&matrix0, &matrix1, &matrix2, &matrix3);

// set to however many you're working with here, up to 8
#define NUMTRELLIS 1

#define numKeys (NUMTRELLIS * 16)

// Connect Trellis Vin to 5V and Ground to ground.
// Connect the INT wire to pin #A2 (can change later!)
#define INTPIN A2
// relay pins
#define relay1 2
#define relay2 3
#define relay3 4
#define relay4 5
#define relay5 6
#define relay6 7
#define relay7 8
#define relay8 9
int i = 0;


// Connect I2C SDA pin to your Arduino SDA line
// Connect I2C SCL pin to your Arduino SCL line
// All Trellises share the SDA, SCL and INT pin! 
// Even 8 tiles use only 3 wires max


void setup() {
    Serial.begin(115200);
    Serial.println("Trellis on");

    // INT pin requires a pullup
    pinMode(INTPIN, INPUT);
    //relay pins
    pinMode(relay1, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT); 
    pinMode(relay4, OUTPUT);
    pinMode(relay5, OUTPUT);
    pinMode(relay6, OUTPUT);
    pinMode(relay7, OUTPUT);
    pinMode(relay8, OUTPUT);

    digitalWrite(INTPIN, HIGH);

    // begin() with the addresses of each panel in order
    // I find it easiest if the addresses are in order
    trellis.begin(0x70);  // only one
    // trellis.begin(0x70, 0x71, 0x72, 0x73);  // or four!

    // light up all the LEDs in order
    for (uint8_t i = 0; i < numKeys; i++) {
        trellis.setLED(i);
        trellis.writeDisplay();
        delay(50);
    }
    // then turn them off
    for (uint8_t i = 0; i < numKeys; i++) {
        trellis.clrLED(i);
        trellis.writeDisplay();
        delay(50);
    }
}

void toggleLight(int i){
    if (trellis.isLED(i))
        trellis.clrLED(i);
    else
        trellis.setLED(i);

}

void loop() {
    delay(30); // 30ms delay is required, dont remove me!
    //init relay states
    static int relay1state = 0;
    static int relay2state = 0;
    static int relay3state = 0;
    static int relay4state = 0;
    static int relay5state = 0;
    static int relay6state = 0;
    static int relay7state = 0;
    static int relay8state = 0;

    if (MODE == MOMENTARY) {
        // If a button was just pressed or released...
        if (trellis.readSwitches()) {
            // go through every button
            for (uint8_t i = 0; i < numKeys; i++) {
                // if it was pressed, turn it on
                if (trellis.justPressed(i)) {
                    Serial.print("v"); Serial.println(i);
                    trellis.setLED(i);
                }
                // if it was released, turn it off
                if (trellis.justReleased(i)) {
                    Serial.print("^"); Serial.println(i);
                    trellis.clrLED(i);
                }
            }
            // tell the trellis to set the LEDs we requested
            trellis.writeDisplay();
        }
    }

    if (MODE == LATCHING) {
        // If a button was just pressed or released...
        if (trellis.readSwitches()) {
            // go through every button
            for (i; i < numKeys; i++) {
                // if it was pressed...
                if (trellis.justPressed(i)) {

                    switch (i)
                    {
                    case '0':
                        relay1state ^= 1; // xor current value with 1 (causes value to toggle)
                        if (relay1state) {
                            delay(100);
                            digitalWrite(relay1, HIGH);
                            toggleLight(i);
                            Serial.println("Outlet 1 on");
                            break;
                        }
                        else {
                            delay(100);
                            digitalWrite(relay1, LOW);
                            toggleLight(i);
                            Serial.println("Outlet 1 off");
                            break;
                        }

                    case '1':
                        relay2state ^= 1; // xor current value with 1 (causes value to toggle)
                        if (relay2state) {
                            delay(100);
                            digitalWrite(relay2, HIGH);
                            toggleLight(i);
                            Serial.println("Outlet 2 on");
                            break;
                        }
                        else {
                            delay(100);
                            digitalWrite(relay2, LOW);
                            toggleLight(i);
                            Serial.println("Outlet 2 off");

                            break;
                        }
                        //...add case 3-8 & 9-15
                    }
                    trellis.writeDisplay();
                }
            }
        }

    }
}
7
  • Relay1 one doesn't work because it's commented out //digitalWrite(relay1, HIGH);. Could you also post the serial output you get?
    – Gerben
    Aug 1 '14 at 19:34
  • I commented it out to test. It should be on. I will correct it in the original post. Output from serial monitor: Opening port Debug on Port open Aug 1 '14 at 19:46
  • Specifically, i'm not seeing the LED's light when I press button 1. Nor am I getting any serial feedback that I included ( when it lights, it prints on the serial). Aug 1 '14 at 19:49
  • You should see "trellis on" in the console. Try adjusting the baud rate (bottom right).
    – Gerben
    Aug 1 '14 at 20:06
  • Yup, that's showing. Aug 1 '14 at 20:08
1

Stepping back from your code problems, why are you using relays? Do you mean electromechanical relays? Those draw a lot of current and you need to wire them with a protection diode so they don't destroy the control circuit when you disconnect the power and the inductive field on the relay coil collapses.

If you're switching AC power, mechanical relays are a reasonable way to go, but for DC like motors/LEDs, etc, it's usually better to use electronic switches of some kind (e.g. transistors).

2
  • I am using the relays to switch on electrical outlets. Mainly for centralized control of the lighting in our office/workspace. It has been done as safe as can be (i.e. fuses in place, proper grounding).I've done this already with a Spark Core controlling 4 relays. I can turn on the relay from the cell network and get my soldering iron preheated, as an example. Aug 2 '14 at 1:07
  • Gotcha. Like I said, for switching AC power, relays are a reasonable choice. Remember to put flyback protection diodes in parallel with your relay coils to prevent damage to your switching transistors.
    – Duncan C
    Aug 2 '14 at 2:43
0

There's an issue with your switch case statement. You switch on int i but your cases use chars like '1'. This is comparing the value of i to the integer value of the character 0 or 1 which are actually 48 and 49 respectively. Check out an ASCII table for more info on this.

Try removing the single quotes and just have something like this:

switch(i) {
    case 0:
        //case 0 stuff
        break;
    case 1:
        //case 1 stuff
        break;
}

This will compare the value of i to the number that you expect.

I suppose another way to fix this would be to add 48 to i every time you check it. I would change the case statements though.

UPDATE

You should be declaring the ready state variables outside of your loop() function and then initializing them in setup(). Something like this:

int ready1state;
void setup() {
    //setup stuff
    ready1state = 0;
}

void loop() {
    //loop stuff
    ready1state ^= 1;
    //use ready1state
}
8
  • That was a stupid mistake on my part. Now I have the relay and the light turning on but doesn't respond when pressed again. (i.e. light stays on, relay stays in position) Aug 1 '14 at 20:07
  • No worries, we all make silly mistakes! As for the lastest problem, does the code enter the else block? Does it ever print out "Outlet 1 off" or "Outlet 2 off"?
    – Doowybbob
    Aug 1 '14 at 20:14
  • No, it doesn't print when it is supposed to switch off. I would gather that the else block isn't being entered. Aug 1 '14 at 20:16
  • Oh, should have seen this earlier. You declare your ready states equal to 0 at the beginning of the loop() function. So every time the code loops, your ready states get set to 0 and then xor'ed with 1 which equals 1. So it will never enter the else statement. Declare them outside of a function (like where your defines are) and then set them equal to 1 in setup(). I'll post an update in my answer.
    – Doowybbob
    Aug 1 '14 at 20:28
  • Made your suggested change. Still looks like it's not getting to the else part. Lights up and relay trips, but after the one press of the button, no other buttons or relays respond (nor does that same one turn off). Aug 1 '14 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.