String & Char readings

So i have been trying to control my blinds that run of 433 mhz. I can control them by manually writing high's and low's but i want a more quicker way to send these codes.

the string code represents the sequence of highs and lows to be send.

This is the code i wrote:

//Define Variables
#define SEND_DATA 4 //Data pin for RF Transmitter
#define ZERO_HIGH 363 //Delay for the high part of a 0 in microseconds
#define ZERO_LOW 726 //Delay for the low part of a 0 in microseconds
#define ONE_HIGH 726 //Delay for the high part of a 1 in microseconds
#define ONE_LOW 363 //Delay for the low part of a 1 in microseconds


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {

String code = "1100111101000001000111011011000100010001";
Serial.println(code);

Serial.println();

for (int i=0; i <= 6; i++) {

  int delayTime;
  for(int k=0;k<20;k++) {
    String code = "1100111101000001000111011011000100010001";
    char code1 = code.charAt(k);
    int highTime;
    int lowTime;
    delayTime = (code1);
    Serial.print(delayTime);
        if (delayTime == 1){
        highTime = ONE_HIGH;
        lowTime = ONE_LOW;
      }
      else {
        highTime = ZERO_HIGH;
        lowTime = ZERO_LOW;
      }
        digitalWrite(SEND_DATA, HIGH);
        delayMicroseconds(highTime);
        digitalWrite(SEND_DATA, LOW);    
        delayMicroseconds(lowTime);

    
  }
  Serial.println();
  Serial.print("Round:");
  Serial.print(i);
  Serial.println();
  delay(10000);
}
}

and in serial monitor i get this :

1100111101000001000111011011000100010001

4949484849494949484948484848484948484849
Round:0
4949484849494949484948484848484948484849
Round:1
4949484849494949484948484848484948484849
Round:2
4949484849494949484948484848484948484849
Round:3
4949484849494949484948484848484948484849
Round:4
4949484849494949484948484848484948484849
Round:5
4949484849494949484948484848484948484849
Round:6

Where is this "4949484849494949484948484848484948484849" coming from ?

when you do

    char code1 = code.charAt(k);

code1 is a character and will be either '0' or '1'.

in ASCII symbol '0' is represented by decimal 48 and the symbol '1' One is represented by decimal 49

when you do

    delayTime = (code1);

you store in delayTime the ASCII code for zero or one, which is 48 or 49

and when you do

    Serial.print(delayTime);

you print 48 or 49, which is the garbage you see :slight_smile:

if you want delayTime to be the integer 1 when code1 as a character was '1'

then instead of doing

    delayTime = (code1);

you need to do

    delayTime = ((int) code1) -48; // 48 is zero in ASCII, so 48-48 = 0 as a number.

and by the way you should not do

    String code = "1100111101000001000111011011000100010001";

every time at the beginning of your for (k ... loop

Thanks!,

delayTime = ((int) code1) -48; // 48 is zero in ASCII, so 48-48 = 0 as a number. did it!

yah, i removed the string code at beginning of k loop, not sure what i was thinking.

So it's working now?

Yes, working perfectly, just integrated it with Ethernet shield to control the blinds over HTTP now. 25 more windows to go :frowning:

Actually, you should not do "String" anything on an Arduino.

aarg's advice is good, as the String class eats a lot of memory. Your original code, after removing the duplicate code string, compiled to 4878 bytes (with no other changes). By changing the String object to a char array, the code size shrinks to 3470 bytes. (If could be made even smaller.)

//Define Variables
#define SEND_DATA 4 //Data pin for RF Transmitter
#define ZERO_HIGH 363 //Delay for the high part of a 0 in microseconds
#define ZERO_LOW 726 //Delay for the low part of a 0 in microseconds
#define ONE_HIGH 726 //Delay for the high part of a 1 in microseconds
#define ONE_LOW 363 //Delay for the low part of a 1 in microseconds


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {

  char code[] = "1100111101000001000111011011000100010001";
  Serial.println(code);

  Serial.println();

  for (int i = 0; i <= 6; i++) {

    int delayTime;
    for (int k = 0; k < 20; k++) {
      char code1 = code[k];
      int highTime;
      int lowTime;
      delayTime = (code1);
      Serial.print(delayTime);
      if (delayTime == 1) {
        highTime = ONE_HIGH;
        lowTime = ONE_LOW;
      }
      else {
        highTime = ZERO_HIGH;
        lowTime = ZERO_LOW;
      }
      digitalWrite(SEND_DATA, HIGH);
      delayMicroseconds(highTime);
      digitalWrite(SEND_DATA, LOW);
      delayMicroseconds(lowTime);


    }
    Serial.println();
    Serial.print("Round:");
    Serial.print(i);
    Serial.println();
    delay(10000);
  }
}

Also, before you post code here, move the cursor to the Source Code window of the IDE and press Ctrl-T. That will refomat the code into a common C style that easier for us to read.