Hi everyone,
I got stuck in a problem. I'm working with (example: 0xFF03) codes for my Infra Red light strips. In my code I want to Serial send the addres (example: 0x2) to turn my LED strips OFF. I got so far to get full Strings trough my serial monitor. So if I send: 0x2, the program would recognise the "0x" at the beginning. Also he turns the value after the "0x" into a "int" and with a snprintf() function he sticks the "0x" in front of it, but then it turns into a "char".
Can anyone help me with a function to write ether the "String" version or the "Char" version of my "0x2" code into a "Byte" or a "uint16_t"? See my code below...
#include <Arduino.h>
#include "PinDefinitionsAndMore.h"
#include <IRremote.h>
String data;
byte code = 0;
void setup() {
Serial.begin(9600);
for(int i=9; i<=12; i++){
pinMode(i, OUTPUT);
}
IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK);
}
//IrSender.sendNEC(sAddress, 0x2, sRepeats);
uint16_t sAddress = 0xEF00;
uint8_t sRepeats = 0;
byte test = 0x2;
void loop(){
while(Serial.available()){
delay(3);
char c = Serial.read();
data += c;
}
if(data.length() > 0){
Switch(data.substring(0, 1).toInt(), data.substring(1));
if(data.substring(0, 2) == "0x"){
//char val[20];
//int input = data.substring(2).toInt();
//snprintf(val, sizeof(val), "0x%X", input);
Serial.println(val, HEX);
IrSender.sendNEC(sAddress, (val, HEX), sRepeats);
}
data = "";
}
}
void Switch(int numb, String state){
int pin = numb + 8;
int intState;
if(state == "on" || state == "ON"){
digitalWrite(pin, HIGH);
}
if(state == "off" || state == "OFF"){
digitalWrite(pin, LOW);
}
}