Pages: [1]   Go Down
Author Topic: Need some tips for code  (Read 326 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
RCSwitch mySwitch = RCSwitch();
This is wrong. You never call the constructor directly.
Code:
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:
  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:
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi end up with this
Code:
#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();
   
    }
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9549
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probably no technical reason to change your original code into an incremental loop. Actually may make your code more difficult to use and modify.
« Last Edit: September 27, 2013, 10:33:17 pm by zoomkat » Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: