// the number of the LED pin const int ledAmbarI = 2; const int ledRojoI = 25; // const int ledAmbarD= 4; const int ledRojoD = 26; // // setting PWM properties const int freq1 = 50; const int ledChannel1 = 0; const int resolution1 = 8; const byte numChars = 32; char receivedChars[numChars]; char tempChars[numChars]; // temporary array for use when parsing // variables to hold the parsed data char comando[numChars] = {0}; int valor1 = 0; int valor2 = 0; int dia = 0; int stop = 0; int left = 0; int right = 0; int inter = 0; boolean newData = false; String edoinput; String sendStrin; String smID; void setup(){ Serial.begin(9600); // configure LED PWM functionalitites ledcSetup(ledChannel1, freq1, resolution1); // attach the channel to the GPIO to be controlled ledcAttachPin(ledRojoI, ledChannel1); ledcAttachPin(ledRojoD, ledChannel1); pinMode(ledAmbarI, OUTPUT); pinMode(ledAmbarD, OUTPUT); Serial.print("START PROGRAM OK"); } void loop(){ recvWithStartEndMarkers(); if (newData == true) { strcpy(tempChars, receivedChars); // this temporary copy is necessary to protect the original data // because strtok() used in parseData() replaces the commas with \0 parseData(); showParsedData(); newData = false; } // increase the LED brightness // ledcWrite(ledChannel, dutyCycle); ledcWrite(ledChannel1, dia+stop); digitalWrite(ledAmbarI, LOW); digitalWrite(ledAmbarD, LOW); delay(300); //ledcWrite(ledChannel1, 255); digitalWrite(ledAmbarI, left+inter); digitalWrite(ledAmbarD, right+inter); delay(300); } //============ void recvWithStartEndMarkers() { static boolean recvInProgress = false; static byte ndx = 0; char startMarker = '<'; char endMarker = '>'; char rc; while (Serial.available() > 0 && newData == false) { rc = Serial.read(); if (recvInProgress == true) { if (rc != endMarker) { receivedChars[ndx] = rc; ndx++; if (ndx >= numChars) { ndx = numChars - 1; } } else { receivedChars[ndx] = '\0'; // terminate the string recvInProgress = false; ndx = 0; newData = true; } } else if (rc == startMarker) { recvInProgress = true; } } } //============ void parseData() { // split the data into its parts char * strtokIndx; // this is used by strtok() as an index strtokIndx = strtok(tempChars,","); // get the first part - the string strcpy(comando, strtokIndx); // copy it to messageFromPC strtokIndx = strtok(NULL, ","); // this continues where the previous call left off valor1 = atoi(strtokIndx); // convert this part to an integer strtokIndx = strtok(NULL, ","); valor2 = atoi(strtokIndx); // convert this part to a float } //============ void showParsedData() { // Serial.print("Mensaje "); // Serial.println(messageFromPC); // Serial.print("Numero de Relevador: "); // Serial.println(numCanal); // Serial.print("Valor Relevador: "); // Serial.println(valueCanal); if (String(comando) == "D"){ //SerialDos.write("COMANDO I "); if (valor1 >= 10) // CAMBIAR EN CADA TARJETA || dia = 20; else dia = 0; Serial.print("dis"); } if (String(comando) == "S"){ //SerialDos.write("COMANDO I "); if (valor1 >= 10) // CAMBIAR EN CADA TARJETA || stop = 235; else stop = 0; Serial.print("stop"); } if (String(comando) == "L"){ //SerialDos.write("COMANDO I "); if (valor1 >= 10) // CAMBIAR EN CADA TARJETA || left = 1; else left = 0; Serial.print("left"); } if (String(comando) == "R"){ //SerialDos.write("COMANDO I "); if (valor1 >= 10) // CAMBIAR EN CADA TARJETA || right = 1; else right = 0; Serial.print("right"); } if (String(comando) == "I"){ //SerialDos.write("COMANDO I "); if (valor1 >= 10) // CAMBIAR EN CADA TARJETA || inter = 1; else inter = 0; Serial.print("inter"); } } void demo(){ }