LED and Buzzer - Code
From RoboWiki
Return back to project page: Remote Controlled Car - Jakub Vojtek
Python code for the Line Following Car project:
int redPin = 7;
int greenPin = 9;
int bluePin = 8;
int buzzerPin = 3;
bool ambulanceMode = false;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
if (command == 'P') {
ambulanceMode = !ambulanceMode;
}
}
if (ambulanceMode) {
ambulanceLightsAndSiren();
} else {
noTone(buzzerPin);
setColor(0, 0, 0);
}
}
void ambulanceLightsAndSiren() {
setColor(255, 0, 0);
for (int i = 700; i <= 800; i++) {
if (i % 50 == 0) {
toggleColor();
}
tone(buzzerPin, i);
delay(15);
}
for (int i = 800; i >= 700; i--) {
if (i % 50 == 0) {
toggleColor();
}
tone(buzzerPin, i);
delay(15);
}
}
void toggleColor() {
static bool red = false;
if (red) {
setColor(0, 255, 0);
} else {
setColor(255, 0, 0);
}
red = !red;
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}