#include "pitches.h"

/* ENA/ENB moved to pins 5 and 6 on 2026-08-16.
 *
 * They were on 13 and 8, and neither is PWM-capable on an Uno — only 3, 5, 6,
 * 9, 10 and 11 are. analogWrite() to a non-PWM pin just sets HIGH above 128,
 * and every value in the speed ladder below is 150 or more, so all ten speed
 * buttons wrote full power. The car drove fine and always flat out, which is
 * why nobody noticed between 2016 and now.
 *
 * IN1-IN4 keep their pins; only the two enables move. This makes the sketch
 * agree with the book, the wiring diagrams and the photographs, which all
 * document 5 and 6 — until now the shipped code contradicted them, and anyone
 * following the guide would have wired enables the sketch never drives, so
 * the car would not have moved at all.
 *
 * ✅ The physical car was re-wired to pins 5 and 6 on 2026-08-16 and verified:
 * speed 1 crawls, speed 9 runs. The speed control works for the first time
 * since 2016.
 */
#define enB 6
#define in4 9
#define in3 10

#define in2 11
#define in1 12
#define enA 5

#define buzzerpin 2

char btsignal;

bool reversing = false;

unsigned long previousMillis = 0;
const long interval = 100; 

void setup() {

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(buzzerpin, OUTPUT);
  
  set_speed(150);
  
  digitalWrite(in1, HIGH);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, HIGH);  

  Serial.begin(9600);
  
}

void loop() {
  
  if(Serial.available()){
   btsignal= Serial.read();
    //Serial.println(t);
  }
 
  if(btsignal== 'F'){
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
  }
   
  else if(btsignal== 'B'){
    reversing = true;
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
  }
   
  else if(btsignal== 'L'){
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW); 
  }
   
  else if(btsignal== 'R'){
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
  }

  else if(btsignal == '0'){
    set_speed(150);
  }

  else if(btsignal == '1'){
    set_speed(150);
  }

  else if(btsignal == '2'){
    set_speed(170);
  }

  else if(btsignal == '3'){
    set_speed(180);
  }

  else if(btsignal == '4'){
    set_speed(200);
  }

  else if(btsignal == '5'){
    set_speed(210);
  }

  else if(btsignal == '6'){
    set_speed(220);
  }

  else if(btsignal == '7'){
    set_speed(230);
  }

  else if(btsignal == '8'){
    set_speed(240);
  }

  else if(btsignal == '9'){
    set_speed(250);
  }

  else if(btsignal == 'q'){
    set_speed(255);
  }

  else if(btsignal == 'W'){
    //frontlight = true;
  }
  
  else if(btsignal == 'w'){
    //frontlight = false;
  }
  
  else if(btsignal == 'V'){
    tone(buzzerpin,2000,1000);  
  }

  else if(btsignal == 'v'){
    tone(buzzerpin,2000,1000);  
  }    

  else if(btsignal == 'U'){
    //backlight = true;  
  }

  else if(btsignal == 'U'){
    //backlight = false;  
  }

  else if(btsignal == 'X'){
    play_happy_birthday();
  }

  else if(btsignal == 'x'){
    play_happy_birthday();
  }
      
  else {
    reversing = false;
    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, HIGH);       
  }

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // Reversing tone
    if (reversing == true) {
      tone(buzzerpin,5000,interval);
    } 
  }    
}

void set_speed(int speed){
  analogWrite(enA, speed);
  analogWrite(enB, speed);  
}

void play_happy_birthday(){
  
  int melodyHappy[] = {NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4,
               NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4,
               NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_D4,
               NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4};

  int timeHappy[] = {4, 4, 2, 2, 2, 1, /*line 1*/
               4, 4, 2, 2, 2, 1, /*line 2*/
               4, 4, 2, 2, 4, 4, 2, 1, /*line 3*/
               4, 4, 2, 2, 2, 1};               
  
  int noteDuration        = 0;
  int pauseBetweenNotes   = 0;
  int sizeMelody          = sizeof(melodyHappy)/sizeof(int);
    
  for (int i = 0; i < sizeMelody; i++) {
 
    noteDuration = 1000 / timeHappy[i];

    tone(buzzerpin, melodyHappy[i], noteDuration);
     
    pauseBetweenNotes = noteDuration * 1.30;
      
    delay(pauseBetweenNotes);
  
  }  
}   
