Difference between revisions of "LED and Buzzer - Code"

From RoboWiki
Jump to: navigation, search
(Created page with "Return back to project page: Line Following Car - Jakub Vojtek Python code for the Line Following Car project: <syntaxhig...")
 
 
Line 1: Line 1:
Return back to project page: [[Spike Prime - Line Following Car - Jakub Vojtek| Line Following Car - Jakub Vojtek]]
+
Return back to project page: [[Spike Prime - Remote Controlled Car - Jakub Vojtek| Remote Controlled Car - Jakub Vojtek]]
  
 
Python code for the Line Following Car project:
 
Python code for the Line Following Car project:

Latest revision as of 11:29, 6 June 2024

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);
}