Go Down

Topic: arduino string to hex (Read 884 times) previous topic - next topic

michael040990

ok i tryed modifing it but now i get more errors?

new code is
Code: [Select]
//Start Defining Tv Code
#define KEY_POWERON 0x750;
#define KEY_POWEROFF 0xF50;
#define KEY_TUNER 0x250;
#define KEY_VIDEO1 0x030;
#define KEY_VIDEO2 0x830;
#define KEY_VIDEO3 0x430;
#define KEY_VIDEO4 0xE30;
#define KEY_VIDEO5 0x130;
#define KEY_VIDEO6 0x930;
#define KEY_POWER 0xA90;
#define KEY_MUTE 0x290;
#define KEY_SLEEP 0x6D0;
#define KEY_ANTENNA 0x550;
#define KEY_DISPLAY 0x5D0;
#define KEY_INPUT 0xA50;
#define KEY_1 0x010;
#define KEY_2 0x810;
#define KEY_3 0x410;
#define KEY_4 0xC10;
#define KEY_5 0x210;
#define KEY_6 0xA10;
#define KEY_7 0x610;
#define KEY_8 0xE10;
#define KEY_9 0x110;
#define KEY_0 0x910;
#define KEY_JUMP 0xDD0;
#define KEY_ENTER 0xD10;
#define KEY_MENU 0x070;
#define KEY_UP 0x2F0;
#define KEY_DOWN 0xAF0;
#define KEY_LEFT 0x2D0;
#define KEY_RIGHT 0xCD0;
#define KEY_OK 0xA70;
#define KEY_VOLUMEUP 0x490;
#define KEY_VOLUMEDOWN 0xC90;
#define KEY_CHANNELUP 0x090;
#define KEY_CHANNELDOWN 0x890;
#define KEY_FREEZE 0x3B0;
#define KEY_PIP 0xDB0;
#define KEY_PICTUREMODE 0x270;
//End Defining Tv Code


#include <IRremote.h>
IRsend irsend;

char inputString[80];         // a char to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  // reserve 2000 bytes for the inputString:

}

//Begin Loop
void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    SendCode(); //start send code void
    // clear the string:
    inputString[0] = '\0';
   
    stringComplete = false;
  }
}
//End Loop



/*
  SerialEvent occurs whenever a new data comes in the
hardware serial RX.  This routine is run between each
time loop() runs, so using delay inside loop can delay
response.  Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    strcat (inputString,inChar);
   
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }

}

// Start Send Tv Code
void SendCode(){
  int value = x2i(inputString);
   
  Debug("inputString",inputString);
  Serial.println(value);

  for (int i = 0; i < 2; i++) {
    irsend.sendSony(value, 12); // Sony TV code
    delay(100);
  }
}
//End Tv Send Code

//Start Hex String To Int
int x2i(char *s)
{
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0';
    }
    else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10;
    }
    else break;
    s++;
  }
  return x;
}
//End Hex String To Int


//start debug
void Debug(String Title, String Value){
  Serial.println();
  Serial.println("---start debug---");
  Serial.println(Title);
  Serial.println(Value);
  Serial.println("---end Debug---");

  //end debug
}



new errors are
BareMinimum.cpp: In function 'void serialEvent()':
BareMinimum:85: error: invalid conversion from 'char' to 'const char*'
BareMinimum:85: error: initializing argument 2 of 'char* strcat(char*, const char*)'

Nick Gammon

You are going to have to do a tiny bit of research about how to use strings in C.

Also this might help:

http://www.gammon.com.au/serial

michael040990

thank you for pointing me into the right direction i got it all working

Code: [Select]
//Start Defining Tv Code
#define KEY_POWERON 0x750;
#define KEY_POWEROFF 0xF50;
#define KEY_TUNER 0x250;
#define KEY_VIDEO1 0x030;
#define KEY_VIDEO2 0x830;
#define KEY_VIDEO3 0x430;
#define KEY_VIDEO4 0xE30;
#define KEY_VIDEO5 0x130;
#define KEY_VIDEO6 0x930;
#define KEY_POWER 0xA90;
#define KEY_MUTE 0x290;
#define KEY_SLEEP 0x6D0;
#define KEY_ANTENNA 0x550;
#define KEY_DISPLAY 0x5D0;
#define KEY_INPUT 0xA50;
#define KEY_1 0x010;
#define KEY_2 0x810;
#define KEY_3 0x410;
#define KEY_4 0xC10;
#define KEY_5 0x210;
#define KEY_6 0xA10;
#define KEY_7 0x610;
#define KEY_8 0xE10;
#define KEY_9 0x110;
#define KEY_0 0x910;
#define KEY_JUMP 0xDD0;
#define KEY_ENTER 0xD10;
#define KEY_MENU 0x070;
#define KEY_UP 0x2F0;
#define KEY_DOWN 0xAF0;
#define KEY_LEFT 0x2D0;
#define KEY_RIGHT 0xCD0;
#define KEY_OK 0xA70;
#define KEY_VOLUMEUP 0x490; //varafied
#define KEY_VOLUMEDOWN 0xC90;
#define KEY_CHANNELUP 0x090;
#define KEY_CHANNELDOWN 0x890;
#define KEY_FREEZE 0x3B0;
#define KEY_PIP 0xDB0;
#define KEY_PICTUREMODE 0x270;
//End Defining Tv Code

#include <IRremote.h>
IRsend irsend;


// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;

void setup ()
{
  Serial.begin(9600);   // Start Serial Port

  pinMode(4,OUTPUT);   //
  digitalWrite(4,HIGH);// Turn Lights on through Relay
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (char * data)
{
  // Start Send Tv Code
   int value = x2i(data);
   
    Serial.println(value);

  for (int i = 0; i < 2; i++) {
    irsend.sendSony(value, 12); // Sony TV code
    delay(100);
  }
  //End Tv Send Code
 
}  // end of process_data


void loop()
{
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  if (Serial.available () > 0)
  {
    char inByte = Serial.read ();

    switch (inByte)
    {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      process_data (input_line);

      // reset buffer for next time
      input_pos = 0; 
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop

//Start Hex String To Int
int x2i(char *s)
{
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0';
    }
    else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10;
    }
    else break;
    s++;
  }
  return x;
}
//End Hex String To Int

//start debug
void Debug(String Title, String Value){
  Serial.println();
  Serial.println("---start debug---");
  Serial.println(Title);
  Serial.println(Value);
  Serial.println("---end Debug---");

  //end debug
}

michael040990

THANKS TO THIS I FINALLY HAVE CABLE ON MY TV YAY :)

Go Up