/********* Rui Santos Complete project details at https://randomnerdtutorials.com *********/ //CODE FOR BUTTON TO TURN ON LIGHT #include #include #include const int BUTTON =15; int BUTTONstate = 0; // Motor A int motor1Pin1 = 12; int motor1Pin2 = 13; unsigned long myTime; unsigned long pastTime; unsigned long eventInterval = 2; //the pins for the same motor need to be set to different values //Motor B int motor2Pin1 = 14; int motor2Pin2 = 2; bool left_wheel_moved=false; bool right_wheels_turn=false; // Setting PWM properties const int freq = 30000; const int pwmChannel = 0; const int resolution = 8; int dutyCycle = 200; int delay_wheel_one_f= delay_wheel_one_f; int delay_wheel_two_f=delay_wheel_two_f; // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); if (value.length() > 0) { Serial.println("*********"); Serial.print("New value: "); for (int i = 0; i < value.length(); i++) Serial.print(value[i]); if(value=="F") { Serial.println("works"); digitalWrite(2,LOW); //TRY 8:both wheels forward digitalWrite(14,HIGH); //TRY 8:both wheels forwarD delay(10); digitalWrite(13,LOW); //TRY 8:both wheels forward digitalWrite(12,HIGH); //TRY 8:both wheels forward delay(50); } Serial.println(); Serial.println("*********"); } } }; void setup() { /*put it as input, so that it can read the state of the button*/ pinMode(BUTTON, INPUT_PULLUP); // sets the pins as outputs: pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(motor2Pin1, OUTPUT); pinMode(motor2Pin2, OUTPUT); //pinMode(enable1Pin, OUTPUT); // configure LED PWM functionalitites ledcSetup(pwmChannel, freq, resolution); // attach the channel to the GPIO to be controlled //ledcAttachPin(enable1Pin, pwmChannel); Serial.begin(115200); // configure LED PWM functionalitites ledcSetup(pwmChannel, freq, resolution); // testing Serial.print("Testing DC Motor..."); Serial.begin(115200); Serial.println("1- Download and install an BLE scanner app in your phone"); Serial.println("2- Scan for BLE devices in the app"); Serial.println("3- Connect to MyESP32"); Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something"); Serial.println("5- See the magic =)"); BLEDevice::init("MyESP32"); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); pCharacteristic->setCallbacks(new MyCallbacks()); pCharacteristic->setValue("Hello World"); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } void forward(){ digitalWrite(2,LOW); //TRY 8:both wheels forward digitalWrite(14,HIGH); //TRY 8:both wheels forwarD delay(10); digitalWrite(13,LOW); //TRY 8:both wheels forward digitalWrite(12,HIGH); //TRY 8:both wheels forward delay(50); } void forward2(){ digitalWrite(2,LOW); //TRY 8:both wheels forward digitalWrite(14,HIGH); //TRY 8:both wheels forwarD delay(10); digitalWrite(13,LOW); //TRY 8:both wheels forward digitalWrite(12,HIGH); //TRY 8:both wheels forward delay(20); } void stop1(){ digitalWrite(2,LOW); //TRY 8:both wheels forward digitalWrite(14,LOW); //TRY 8:both wheels forwarD digitalWrite(13,LOW); //TRY 8:both wheels forward digitalWrite(12,LOW); //TRY 8:both wheels forward delay(1000); } void first_right(){ digitalWrite(2,LOW); //TRY 8:both wheels forward digitalWrite(14,HIGH); //TRY 8:both wheels forwarD delay(100); digitalWrite(13,LOW); //TRY 8:both wheels forward digitalWrite(12,HIGH); //TRY 8:both wheels forward delay(1000); } void first_left(){ digitalWrite(2,HIGH); //TRY 8:both wheels forward digitalWrite(14,LOW); //TRY 8:both wheels forwarD delay(100); digitalWrite(13,HIGH); //TRY 8:both wheels forward digitalWrite(12,LOW); //TRY 8:both wheels forward delay(500); } void back(){ digitalWrite(2,HIGH); //TRY 8:both wheels forward digitalWrite(14,LOW); //TRY 8:both wheels forwarD delay(10); digitalWrite(13,HIGH); //TRY 8:both wheels forward digitalWrite(12,LOW); //TRY 8:both wheels forward delay(100); } void loop() { //Motor B if(digitalRead(BUTTON)==HIGH){ Serial.println("first forward"); forward(); stop1(); Serial.println("second forward"); forward(); stop1(); Serial.println("third forward"); forward(); stop1(); Serial.println("fourth forward"); forward(); stop1(); Serial.println("fourth forward"); forward(); stop1(); Serial.println("fourth forward"); forward(); stop1(); Serial.println("fourth forward"); forward(); stop1(); Serial.println("first right"); first_right(); stop1(); first_right(); stop1(); first_right(); stop1(); first_right(); stop1(); first_right(); stop1(); first_left(); stop1(); first_left(); stop1(); stop1(); Serial.println("fourth forward"); forward(); stop1(); Serial.println("first right"); first_right(); stop1(); first_right(); stop1(); first_right(); stop1(); first_right(); stop1(); first_right(); stop1(); first_left(); stop1(); first_left(); stop1(); } else{ digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); digitalWrite(motor1Pin1,LOW); digitalWrite(motor1Pin2, LOW); } }