Go Down

Topic: Need some tips for code (Read 417 times) previous topic - next topic

smallpoul

Hi all,
        i have a sketch that receive code that is a string to control relay, the code look like this, 9910 9911. Then the string trim
string activate = 99, string sensorID = 1, string state = 0. here the code:
Code: [Select]
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

int Relay_1 = 13;
int Relay_2 = 4;
int Relay_3 = 5;
int Relay_4 = 6;
int Relay_5 = 7;
int Relay_6 = 8;
int Relay_7 = 9;
int Relay_8 = 10;
int value;
int protocole;
String sensorID;
String state;
String activate;

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  pinMode(Relay_5, OUTPUT);
  pinMode(Relay_6, OUTPUT);
  pinMode(Relay_7, OUTPUT);
  pinMode(Relay_8, OUTPUT);
}

void loop() {
  if (mySwitch.available()) {
   
    value = mySwitch.getReceivedValue();
    protocole =mySwitch.getReceivedProtocol();
    String code = String(value);
    activate = code.substring(0,2);
    if ((activate == "99") && (protocole == 1)){
    Serial.println(value);
    int length = code.length();
    sensorID = code.substring(2,length-1);
    state    = code.substring(length-1);
    Serial.println(state);
    if (state == "1"){
       digitalWrite(Here were when sensorID is 1 i want Relay_1, HIGH);
    }
    else if (state == "0"){
      digitalWrite(Relay_1, LOW);
    }
    delay(1500);
    mySwitch.resetAvailable();
   
    }
  }
}
   
So sensorID will be 1=Relay_1, 2 Relay_2 and so on. Where i'm stock is how to replace the Relay_1 by the string.Not sure if i'm clear enough but this is my problem, when sensorid is 1, relay_1 pin will be use, when 2 relay_2 pin will be used.
Thanks

PaulS

Code: [Select]
RCSwitch mySwitch = RCSwitch();
This is wrong. You never call the constructor directly.
Code: [Select]
RCSwith mySwitch;
creates an instance of the class.

Sending data with delimiters makes for easier parsing. Sending start and end markers also helps. So, send "<99,1,0>", instead of "9910".

The biggest problem you have is this:
Code: [Select]
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  pinMode(Relay_5, OUTPUT);
  pinMode(Relay_6, OUTPUT);
  pinMode(Relay_7, OUTPUT);
  pinMode(Relay_8, OUTPUT);

Replace that mess with:
Code: [Select]
for(byte i=0; i<8; i++)
{
   pinMode(Relay[i], OUTPUT);
}


Of course, you'll need to put the pin numbers in an array, called Relay. Then, turning on or off the ith relay will be simple.

smallpoul

Thanks,
           so the bytes will correspond to the sensorID? if so could i skip the bytes 0 since sensorID will correspond to the bytes?
Again thanks

smallpoul

Hi end up with this
Code: [Select]
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

int Relay[]={0,3,4,5,6,7,8,9,13};
int value;
int protocole;
String sensorID;
String state;
String activate;

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  for(byte i=1; i<9; i++)
{
   pinMode(Relay[i], OUTPUT);
}
}

void loop() {
  if (mySwitch.available()) {
   
    value = mySwitch.getReceivedValue();
    protocole =mySwitch.getReceivedProtocol();
    String code = String(value);
    activate = code.substring(0,2);
    if ((activate == "99") && (protocole == 1)){
    int length = code.length();
    sensorID = code.substring(2,length-1);
    state    = code.substring(length-1);
    if (state == "1"){
       int test = sensorID.toInt();
       digitalWrite(Relay[test], HIGH);
    }
    else if (state == "0"){
      int test = sensorID.toInt();
      digitalWrite(Relay[test], LOW);
    }
    delay(500);
    mySwitch.resetAvailable();
   
    }
  }
}

PaulS

Code: [Select]
int Relay[]={0,3,4,5,6,7,8,9,13};
int value;
int protocole;
String sensorID;
String state;
String activate;

void setup() {
  Serial.begin(9600);

Is pin 0 used for Serial or a relay? Both is the wrong answer. You can subtract one from the input string (no need to waste resources on Strings) after converting to int, if you don't like 0 as a sensor ID.

zoomkat

#5
Sep 28, 2013, 03:46 am Last Edit: Sep 28, 2013, 05:33 am by zoomkat Reason: 1
Probably no technical reason to change your original code into an incremental loop. Actually may make your code more difficult to use and modify.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up