/*V9To do:check powersupply corrumpeerd waitRichtingaanwijzers werken maar nog remote aan te sturenRemote aansturen snelheid, maxspeed?Done:check Powersupply still on delay but ok like this, threshold still to be setAccelerate, Cruise and brake workingBlauw Flash werkendDelay na break okThe ESP module uses a different numbering as the Arduino's.Port GPIO numberD0 16 CruiseD1 5 FlashD2 4 stopplace (IR receive Bottom)D3 0. speed motorD4 2. Brake LedD5 14.IR input FrontD6 12 Blink LeftD7 13 Blink RightD8 15A0 A0 Power supply check*/#include <IRremote.h>int RECEIVER_PIN = 14; // IR input Frontint analogPin = A0; // input battery value divided by 3, adaption to the max of the ESP8266 miniint val = 0; // variable to store the value readint maxspeed = 250; // Max allowed speedint minspeed = 0; // Minimal speedint speed; // stores actual speedint currentspeed; // remembers actual speedint flash; // Blue Flashing light on top Cabinint stopPlace; // stopPlace detected, port 4int stop = 1; // remembers detected stopplaceint wait = 0; // WaitTime on stopplaceint distance; // car detects another vehicle in front, port 14const int blinkLeft = (12); // blinker Left, port 12. (D6)const int blinkRight = (13); // blinker Right, port 13 (D7)const int FlashLed = (5); // Flash lights, port 5 (D1)int waitTime = 2000; // Time the vehicle waits before increasing speed againint ledBlink = LOW; // ledState used to set the BlinkLEDint ledFlash = LOW; // ledState used to set the FlashLED//const int startMillis = millis(); // initial start time // initial start timeunsigned long previousMillisBrake = 0; // will store last time Brake was updatedconst long brakeInterval = 200; // Brake intervalunsigned long previousMillisBlink = 0; // will store last time Blink was updatedconst long blinkInterval = 300; // Blink intervalunsigned long previousMillisAccelerate = 0; // will store last time Accelerate was updatedconst long AccelerateInterval = 200; // Accelerate intervalunsigned long previousMillisWait = 0; // will store last time Waittime was updatedconst long WaitInterval = 5000; // Waittime intervalunsigned long previousMillisCruiseSlower = 0; // will store last time CruiseSlowdown was updatedconst long CruiseSlowerInterval = 300; // CruiseSlower intervalunsigned long previousMillisCruiseFaster = 0; // will store last time CruiseFaster was updatedconst long CruiseFasterInterval = 300; // Cruise Faster intervalunsigned long previousMillisFlash = 0; // will store last time Flash lights was updatedconst long FlashInterval = 1000; // FlasLight intervalvoid setup() { Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); IrReceiver.begin(RECEIVER_PIN); pinMode(4, INPUT); // IR sensor StopPlace bottom vehicle, HIGH = stop pinMode(16, INPUT); // IR sensor "adaptive" cruise control front vehicle, HIGH = stop pinMode(14, INPUT); // IR input Blink pinMode(5, OUTPUT); // Flash pinMode(0, OUTPUT); // speed motor pinMode(2, OUTPUT); // Brake Led pinMode(12, OUTPUT); // blinker Left pinMode(13, OUTPUT); // blinker Right}void loop() { //checkPowerSupply(); // brakelights flashing if power supply is 20% down Accelerate(); // car increases speed Brake(); // car detects a stopplace Wait(); // Wait time after detecting stopplace checkDistance(); // car detects another vehicle in front blinker(); // car detects another vehicle in front FlashLeds(); // blue Flash Leds constantly on}void checkPowerSupply() { val = analogRead(analogPin); // read the input pin //Serial.print("voedingsspanning; "); //Serial.print("value: "); //Serial.println(val); float voltage = val; //Serial.print(voltage); // debug value //Serial.println(" V"); //delay(10); if (val < 500) { // execution of the Sketch stops on battery low, threshold to be determined Serial.println("Battery low"); digitalWrite(2, HIGH); delay(500); digitalWrite(2, LOW); delay(500); } else { //Serial.println("Battery OK"); // remove, in case if statement is changed into while }}void Accelerate() { //Serial.print ("Acc"); stopPlace = digitalRead(4); distance = digitalRead(16); unsigned long currentMillis = millis(); if (currentMillis - previousMillisAccelerate >= AccelerateInterval) { previousMillisAccelerate = currentMillis; if (speed < maxspeed) { //if (stopPlace == 0 ||distance==0) { if (stop == 1 && distance == 1 && wait == 0) { //Serial.print("wait: "); //Serial.println(wait); //Serial.print("distance: "); //Serial.println(distance); digitalWrite(2, LOW); //Brake off speed = speed + 10; currentspeed = speed; analogWrite(0, speed); Serial.print("speedAccelerate :"); Serial.println(speed); Serial.println(""); //delay(300); //break; // bail out on sensor detect //} else { } } }}void checkDistance() { int Distance = digitalRead(16); //Serial.println(Distance); if (Distance == 0) { //while (Distance ==0) //Serial.println("obstacle in front"); Cruise(); //Serial.println (Distance); //delay(500); }}void Brake() { stopPlace = digitalRead(4); if (stopPlace == 0 && distance == 1 && wait == 0) { // if a stopplace is passed the car slows down to 0, Distance should be not active, //in active distance state its handled there stop = 0; digitalWrite(2, HIGH); //Brake on } unsigned long currentMillis = millis(); if (currentMillis - previousMillisBrake >= brakeInterval) { previousMillisBrake = currentMillis; if (stop == 0) { if (speed >= 10) { speed = speed - 10; analogWrite(0, speed); Serial.print("speedBrake: "); Serial.println(speed); digitalWrite(2, HIGH); //Brake on currentspeed = speed; } } if (speed < 10) { stop = 1; // reset wait = 1; } }}void Cruise() { int distance = digitalRead(16); unsigned long currentMillis = millis(); if (currentMillis - previousMillisCruiseSlower >= CruiseSlowerInterval) { previousMillisCruiseSlower = currentMillis; if (distance == 0) { digitalWrite(2, HIGH); //Brake on //Serial.println("Slower"); speed = (speed - 10); analogWrite(0, speed); Serial.print("speedCruise: "); Serial.println(speed); } } if (currentMillis - previousMillisCruiseFaster >= CruiseFasterInterval) { previousMillisCruiseFaster = currentMillis; if (distance == 1) { digitalWrite(2, LOW); //Brake off Serial.println("Faster"); speed = (speed + 10); analogWrite(0, speed); } }}/* void blinker() {//if(IrReceiver.decode()){ //Serial.println(IrReceiver.decodedIRData.command,HEX); //switch(IrReceiver.decodedIRData.command){ //case 0x10: //LeftBlink Serial.println("LeftBlink"); currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started) if (currentMillis - startMillis >= period) //test whether the period has elapsed { digitalWrite(13, !digitalRead(13)); //if so, change the state of the LED. Uses a neat trick to change the state startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state. //digitalWrite(LED_BUILTIN, HIGH); //digitalWrite(12,HIGH); //delay (200); //digitalWrite(12,LOW); //delay(200); // break; } //case 0x20: //RightBlink Serial.println("RightBlink") ; currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started) if (currentMillis - startMillis >= period) //test whether the period has elapsed { digitalWrite(12, !digitalRead(12)); //if so, change the state of the LED. Uses a neat trick to change the state startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state. //digitalWrite(LED_BUILTIN, HIGH); //digitalWrite(12,HIGH); //delay (200); //digitalWrite(12,LOW); //delay(200); // break; } // case 0x30: // default: Serial.println("Ooops, wrong key!"); //} //IrReceiver.resume(); //Moet dit hier staan??????????????????? } //}*/void blinker() { unsigned long currentMillis = millis(); if (currentMillis - previousMillisBlink >= blinkInterval) { previousMillisBlink = currentMillis; digitalWrite(12, ledBlink); if (ledBlink == LOW) { ledBlink = HIGH; } else { ledBlink = LOW; } digitalWrite(13, ledBlink); }}void FlashLeds() { unsigned long currentMillis = millis(); if (currentMillis - previousMillisFlash >= FlashInterval) { previousMillisFlash = currentMillis; digitalWrite(5, ledFlash); if (ledFlash == LOW) { ledFlash = HIGH; } else { ledFlash = LOW; } }}void Wait() { unsigned long currentMillis = millis(); //wait = 0; if (currentMillis - previousMillisWait >= WaitInterval) { previousMillisWait = currentMillis; Serial.println("wait"); wait = 0; //return; //} else { // wait = 1; }}