Telefonul comunică cu modulul Bluetooth, care, la rândul său, trimite comenzile către placa de control. În funcție de comanda primită, placa va efectua următoarele acțiuni:
La apăsarea butonului switch, va avea loc schimbarea modului de viteză și a culorii LED-ului RGB.
void switchNitro() {
if (PIND & (1 << NITRO_PIN)) {
nitro = false;
} else {
nitro = true;
}
}
void setup() {
Serial.begin(9600);
// Input pullup
DDRD &= ~(1 << NITRO_PIN);
PORTD |= (1 << NITRO_PIN);
DDRD |= (1 << HONK_PIN);
DDRD |= (1 << LED_GREEN_PIN);
DDRD |= (1 << LED_RED_PIN);
DDRB |= (1 << UP_LEFT_PIN);
DDRB |= (1 << DOWN_LEFT_PIN);
DDRB |= (1 << DOWN_RIGHT_PIN);
DDRB |= (1 << UP_RIGHT_PIN);
attachInterrupt(digitalPinToInterrupt(NITRO_PIN), switchNitro, CHANGE);
// Init nitro
nitro = false;
// Init RGB LED
PORTD &= ~(1 << LED_RED_PIN);
PORTD |= (1 << LED_GREEN_PIN);
// Init wheels pins
PORTB &= ~(1 << UP_LEFT_PIN);
PORTB &= ~(1 << DOWN_LEFT_PIN);
PORTB &= ~(1 << DOWN_RIGHT_PIN);
PORTB &= ~(1 << UP_RIGHT_PIN);
}
void loop() {
if (Serial.available() > 0) {
cmd = Serial.read();
} else {
cmd = None;
}
if (cmd == Start_Honk) {
tone(HONK_PIN, 300);
} else if (cmd == Stop_Honk){
noTone(HONK_PIN);
}
if (cmd == Nitro_On) {
nitro = true;
} else if (cmd == Nitro_Off) {
nitro = false;
}
if (cmd == Up) {
if (nitro) {
PORTB |= (1 << UP_LEFT_PIN);
PORTB |= (1 << UP_RIGHT_PIN);
} else {
analogWrite(UP_LEFT_PIN_ARDUINO, 180);
analogWrite(UP_RIGHT_PIN_ARDUINO, 180);
}
PORTB &= ~(1 << DOWN_LEFT_PIN);
PORTB &= ~(1 << DOWN_RIGHT_PIN);
} else if (cmd == Down) {
digitalWrite(UP_LEFT_PIN_ARDUINO, LOW);
digitalWrite(UP_RIGHT_PIN_ARDUINO, LOW);
PORTB |= (1 << DOWN_LEFT_PIN);
PORTB |= (1 << DOWN_RIGHT_PIN);
} else if (cmd == Left) {
digitalWrite(UP_LEFT_PIN_ARDUINO, LOW);
if (nitro) {
PORTB |= (1 << UP_RIGHT_PIN);
} else {
analogWrite(UP_RIGHT_PIN_ARDUINO, 180);
}
PORTB &= ~(1 << DOWN_LEFT_PIN);
PORTB &= ~(1 << DOWN_RIGHT_PIN);
} else if (cmd == Right) {
if (nitro) {
PORTB |= (1 << UP_LEFT_PIN);
} else {
analogWrite(UP_LEFT_PIN_ARDUINO, 180);
}
digitalWrite(UP_RIGHT_PIN_ARDUINO, LOW);
PORTB &= ~(1 << DOWN_LEFT_PIN);
PORTB &= ~(1 << DOWN_RIGHT_PIN);
} else if(cmd == Center) {
digitalWrite(UP_LEFT_PIN_ARDUINO, LOW);
digitalWrite(UP_RIGHT_PIN_ARDUINO, LOW);
PORTB &= ~(1 << DOWN_LEFT_PIN);
PORTB &= ~(1 << DOWN_RIGHT_PIN);
}
if (nitro) {
PORTD |= (1 << LED_RED_PIN);
PORTD &= ~(1 << LED_GREEN_PIN);
} else {
PORTD &= ~(1 << LED_RED_PIN);
PORTD |= (1 << LED_GREEN_PIN);
}
}