Grove - 6-Position DIP Switch

The code for Grove - 6-Position DIP Switch is not easy for beginners. Is it possible to get a code that output the status of every switch to the serial monitor? Thanks

Hi,



Can you share the existing code/ code repo link!

[code]#include “Grove_Multi_Switch.h”

GroveMultiSwitch mswitch[1];
const char* grove_5way_tactile_keys[] = {
“KEY A”,
“KEY B”,
“KEY C”,
“KEY D”,
“KEY E”,
};
const char* grove_6pos_dip_switch_keys[] = {
“POS 1”,
“POS 2”,
“POS 3”,
“POS 4”,
“POS 5”,
“POS 6”,
};

const char** key_names;

int deviceDetect(void) {
if (!mswitch->begin()) {
Serial.println("***** Device probe failed *****");
return -1;
}

Serial.println("***** Device probe OK *****");
if (PID_VAL(mswitch->getDevID()) == PID_5_WAY_TACTILE_SWITCH) {
    Serial.println("Grove 5-Way Tactile Switch Inserted!");
    key_names = grove_5way_tactile_keys;
} else if (PID_VAL(mswitch->getDevID()) == PID_6_POS_DIP_SWITCH) {
    Serial.println("Grove 6-Position DIP Switch Inserted!");
    key_names = grove_6pos_dip_switch_keys;
}

// enable event detection
mswitch->setEventMode(true);

// report device model
Serial.print("A ");
Serial.print(mswitch->getSwitchCount());
Serial.print(" Button/Switch Device ");
Serial.println(mswitch->getDevVer());
return 0;

}

void setup()
{
Serial.begin(115200);
Serial.println(“Grove Multi Switch”);

// Initial device probe
if (deviceDetect() < 0) {
    Serial.println("Insert Grove 5-Way Tactile");
    Serial.println("or Grove 6-Position DIP Switch");
    for (;;);
}

return;

}

void loop()
{
GroveMultiSwitch::ButtonEvent_t* evt;

delay(1);

evt = mswitch->getEvent();
if (!evt) {
    // dynamic device probe
    deviceDetect();
    delay(1000);
    return;
}

if (!(evt->event & GroveMultiSwitch::BTN_EV_HAS_EVENT)) {
    #if 0
    Serial.print("No event, errno = ");
    Serial.println(mswitch->errno);
    #endif
    return;
}

for (int i = 0; i < mswitch->getSwitchCount(); i++) {
    Serial.print(key_names[i]);
    Serial.print(": RAW - ");
    Serial.print((evt->button[i] & GroveMultiSwitch::BTN_EV_RAW_STATUS)?
                 "HIGH ": "LOW ");
    if (PID_VAL(mswitch->getDevID()) == PID_5_WAY_TACTILE_SWITCH) {
        Serial.print((evt->button[i] & GroveMultiSwitch::BTN_EV_RAW_STATUS)?
                     "RELEASED ": "PRESSED ");
    } else if (PID_VAL(mswitch->getDevID()) == PID_6_POS_DIP_SWITCH) {
        Serial.print((evt->button[i] & GroveMultiSwitch::BTN_EV_RAW_STATUS)?
                     "OFF ": "ON ");
    }
    Serial.println("");
}

for (int i = 0; i < mswitch->getSwitchCount(); i++) {
    if (evt->button[i] & ~GroveMultiSwitch::BTN_EV_RAW_STATUS) {
        Serial.println("");
        Serial.print(key_names[i]);
        Serial.print(": EVENT - ");
    }
    if (evt->button[i] & GroveMultiSwitch::BTN_EV_SINGLE_CLICK) {
        Serial.print("SINGLE-CLICK ");
    }
    if (evt->button[i] & GroveMultiSwitch::BTN_EV_DOUBLE_CLICK) {
        Serial.print("DOUBLE-CLICK ");
    }
    if (evt->button[i] & GroveMultiSwitch::BTN_EV_LONG_PRESS) {
        Serial.print("LONG-PRESS ");
    }
    if (evt->button[i] & GroveMultiSwitch::BTN_EV_LEVEL_CHANGED) {
        Serial.print("LEVEL-CHANGED ");
    }
}
Serial.println("");

}[/code]

Hi,



so right you getting the data in serial monitor right? or what kind of output you need?

I wish to have a variable showing the switches on numbers. Thanks

I think what alirazi was looking for is the same thing I am. A polling solution, not event driven.

I just want to be able to read the dip switch at my own choosing and take action based on it’s configuration.
I would rather not waste the overhead of having an interrupt when, for my application, I will only read the DIP switch once in the morning to run the day’s events based on it’s positioning.

Thank you!

This seems to do fine reading the 6-position DIP switch every 5 seconds and tell you what the positions are. (“polling”)
For applications that favor a polling method over an event driven method

Note I removed all the 5 button references, this will only work for 6position DIP.

#include “Grove_Multi_Switch.h”

GroveMultiSwitch mswitch[1];

const char* grove_6pos_dip_switch_keys[] = {
“POS 1”,
“POS 2”,
“POS 3”,
“POS 4”,
“POS 5”,
“POS 6”,
};

const char** key_names;

int deviceDetect(void) {
if (!mswitch->begin()) {
Serial.println("***** Device probe failed *****");
return -1;
}else
{
Serial.println("***Device probe OK *****");
Serial.println(“Grove 6-Position DIP Switch Inserted!”);
key_names = grove_6pos_dip_switch_keys;
}
return 0;
}

void setup()
{
Serial.begin(9600);
// Initial device probe
if (deviceDetect() < 0) {
Serial.println(“Insert Grove 6-Position DIP Switch”);
for (;;);
}
return;
}

void loop()
{
GroveMultiSwitch::ButtonEvent_t* evt;
delay(1);
evt = mswitch->getEvent();
Serial.println(“Reading DIP switch values:”);

for (int i = 0; i < 6 ; i++) {
Serial.print(key_names[i]);
Serial.print(": RAW - ");
Serial.print((evt->button[i] &GroveMultiSwitch::BTN_EV_RAW_STATUS)? "HIGH ": "LOW ");
Serial.print((evt->button[i] & GroveMultiSwitch::BTN_EV_RAW_STATUS)? "OFF ": “ON “);
Serial.println(””);

}

delay(5000);
}

Hi everyone,
I connected groove dip switch 6 to the I2C connector of a MKR1010 arduino
I tried multiple codes but I always get this error:

src\Grove_Multi_Switch.h:159:5: error: ‘TwoWire’ does not name a type; did you mean ‘TwoWire_h’?

i used the library downloaded from the offical github repository.

Does anyone have any suggestions please?
thanks in advance for your help

I get exactly the same error. Example program will not compile.
I also want a simple way to read the switch and get the value of the switch. No need for the fancy stuff.