Voila j'ai un petit soucis. J'ai modifier un robosapien en lui ajoutant une carte arduino 2009 .
Mon programme permet , soit de faire echo de la telecommande d'origine, soit de prendre le controle avec l'arduino.
Tout fonctionne bien, que ce soit avec la telecommande ou en envoyant des commande avec l'arduino ( interphaser entre le recepteur IR et la carte mere du robot ) .
Cependant pour mes test, je souhaite entrer des commandes dans le moniteur serie et c'est la que ça cloche :
L'entrée est bien stockée dans ma variable string , mais la condition if ne semble pas etre respectée pour que le changement de la valeur a envoyer a l' IR du robot se fasse :
if (inputString == "RSTurnRight") serialCommand= 0x80;
Avec des " serial.print " j'ai pu débugger et voir qu'effectivement la valeur de serialCommand ne change pas .
J'ai essayé :
if (inputString == "RSTurnRight")
{
serialCommand= 0x80;
}
Mais ça ne change rien ...
Quelqu'un a une idée de mon erreur ?
Voici le code complet :
int IRIn = 2; // We will use an interrupt int IROut= 3; // Where the echoed command will be sent from boolean RSEcho=true; // Should Arduino Echo RS commands boolean RSUsed=true; // Has the last command been used volatile int RSBit=9; // Total bits of data volatile int RSCommand; // Single byte command from IR int bitTime=516; // Bit time (Theoretically 833 but 516) // works for transmission and is faster int last; // Previous command from IR String inputString = ""; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete int serialCommand(0xEF); // Command sent via serial void setup() { pinMode(IRIn, INPUT); pinMode(IROut, OUTPUT); pinMode(10,OUTPUT); digitalWrite(IROut,HIGH); attachInterrupt(0,RSReadCommand,RISING); last=0xEF; // Set last command as " noOp " Serial.begin(115200); } // Receive a bit at a time. void RSReadCommand() { delayMicroseconds(833+208); // about 1 1/4 bit times int bit=digitalRead(IRIn); if (RSBit==9) { // Must be start of new command RSCommand=0; RSBit=0; RSUsed=true; } if (RSBit<8) { RSCommand<<=1; RSCommand|=bit; } RSBit++; if (RSBit==9) RSUsed=false; } // send the whole 8 bits void RSSendCommand(int command) { digitalWrite(IROut,LOW); delayMicroseconds(8*bitTime); for (int i=0;i<8;i++) { digitalWrite(IROut,HIGH); delayMicroseconds(bitTime); if ((command & 128) !=0) delayMicroseconds(3*bitTime); digitalWrite(IROut,LOW); delayMicroseconds(bitTime); command <<= 1; } digitalWrite(IROut,HIGH); delay(250); // Give a 1/4 sec before next } void loop() { if (!RSUsed) { if (RSCommand==0x8E && last==0x8E) { RSEcho=!RSEcho; } last=RSCommand; // print code received from the remote Serial.print("IR Code : "); Serial.println(RSCommand); if (!RSEcho) { digitalWrite(10,HIGH); // Turn on LED let us know we have control our wedge Serial.println("Arduino control ON "); } else { digitalWrite(10,LOW); // No longer in control Serial.println("Arduino control OFF "); RSSendCommand(RSCommand); } RSUsed=true; } while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar == '\n') { Serial.print ("inputString value: " ); Serial.println (inputString); if (inputString == "RSTurnRight") serialCommand= 0x80; if (inputString == "RSRightArmUp") serialCommand= 0x81; if (inputString == "RSRightArmOut") serialCommand= 0x82; if (inputString == "RSTiltBodyRight") serialCommand= 0x83; if (inputString == "RSRightArmDown") serialCommand= 0x84; if (inputString == "RSRightArmIn") serialCommand= 0x85; if (inputString == "RSWalkForward") serialCommand= 0x86; if (inputString == "RSWalkBackward") serialCommand= 0x87; if (inputString == "RSTurnLeft") serialCommand= 0x88; if (inputString == "RSLeftArmUp") serialCommand= 0x89; if (inputString == "RSLeftArmOut") serialCommand= 0x8A; if (inputString == "RSTiltBodyLeft") serialCommand= 0x8B; if (inputString == "RSLeftArmDown") serialCommand= 0x8C; if (inputString == "RSLeftArmIn") serialCommand= 0x8D; if (inputString == "RSStop") serialCommand= 0x8E; if (inputString == "RSWakeUp") serialCommand= 0xB1; if (inputString == "RSBurp") serialCommand= 0xC2; if (inputString == "RSRightHandStrike") serialCommand= 0xC0; if (inputString == "RSNoOp") serialCommand= 0xEF; Serial.print (" Value sent : " ); Serial.println (serialCommand,HEX); RSSendCommand(serialCommand); Serial.println (" Command sent to robosapien " ); // clear the string: inputString = ""; } } }