Paso 15: El código - reciver
El código receptor sólo recibir los datos y poner los valores a servos, esc o pwm controlador de th, luces y esas cosas.
Aquí hice una (noSignal) para proteger el coche en caso de si el control remoto se detiene o se quede sin batería, o simplemente perder la señal. El noSignal() poner el dirección recta y rompe el coche y también comienza a parpadear los faros. noSignal() entrar cuando el tiempo desde el último valor recibido es mayor entonces 800 milisegundos.
void loop() { reciveData(); timeRcv = millis() - lastTimeRcv; //Serial.println(timeRcv); if (timeRcv < 1000){ RPM = (throttle/10)*100; ///// digitalWrite(headLight, light); stearServo.write(stear); breakServo.write(break_); if(reverse == 0){ analogWrite(thForward, throttle); analogWrite(thReverse, 0); } else if (reverse ==1){ analogWrite(thReverse, throttle); analogWrite(thForward, 0); } } else{ noSignal(); Serial.println(timeRcv); }}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void noSignal(){ analogWrite(thForward, 0); analogWrite(thReverse, 0); breakServo.write(65); //servo val -- full break stearServo.write(108); //servo val -- center Serial.print("NoSignal"); unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval) { previousMillis = currentMillis; if (ledState == 0){ ledState = 1; } else{ ledState = 0; } digitalWrite(headLight, ledState); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void reciveData(){ while (Serial.available() > 0){ int x = Serial.parseInt(); if(x > 0){throttle = x-1;} x = Serial.parseInt(); if (x > 0){break_ = x-1;} x = Serial.parseInt(); if (x > 0){stear = x-1;} x = Serial.parseInt(); if (x > 0){reverse = x-1;} x = Serial.parseInt(); if (x > 0){light = x-1;} delay(5); if(x == (light + 1)){ //Serial.println("end if"); for(;Serial.available();){ char y = Serial.read(); } } delay(5); recived = 1; lastTimeRcv = millis(); } if (recived){ sendData(); }}