Go Down

Topic: String & Char readings (Read 450 times) previous topic - next topic

peashooter

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:

Code: [Select]
//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 :

Code: [Select]
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 ?


J-M-L

#1
Jun 25, 2016, 07:23 pm Last Edit: Jun 25, 2016, 07:28 pm by J-M-L
when you do

Code: [Select]
    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

Code: [Select]
    delayTime = (code1);


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

and when you do

Code: [Select]
    Serial.print(delayTime);


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


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

then instead of doing

Code: [Select]
    delayTime = (code1);


you need to do

Code: [Select]
    delayTime = ((int) code1) -48; // 48 is zero in ASCII, so 48-48 = 0 as a number.
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

J-M-L

and by the way you should not do

Code: [Select]
    String code = "1100111101000001000111011011000100010001";


every time at the beginning of your for (k ...  loop
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

peashooter

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.

J-M-L

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

peashooter

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

aarg

Actually, you should not do "String" anything on an Arduino.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

econjack

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.)

Code: [Select]

//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.

Go Up