Last active
October 12, 2025 07:18
-
-
Save danielbreves/a80a8dc2fe2254227bd38510ffd859fa to your computer and use it in GitHub Desktop.
matthews-the-mblocker.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "MeOrion.h" | |
| MeUltrasonicSensor ultraSensor(PORT_7); /* Ultrasonic module can ONLY be connected to port 3, 4, 6, 7, 8 of base shield. */ | |
| MeBuzzer buzzer; | |
| MeDCMotor leftMotor(M1); | |
| MeDCMotor rightMotor(M2); | |
| uint8_t motorSpeed = 255; | |
| #define NOTE_C5 523 | |
| #define NOTE_E5 659 | |
| #define NOTE_G5 784 | |
| #define NOTE_A4 440 | |
| #define NOTE_F4 349 | |
| #define NOTE_D4 294 | |
| // Downward annoyed tones with final glitchy buzz | |
| void playSadBeep() { | |
| int notes[] = { NOTE_G5, NOTE_F4, NOTE_D4, NOTE_A4 }; | |
| int durations[] = { 250, 200, 180, 300 }; | |
| for (int i = 0; i < 4; i++) { | |
| int pitch = notes[i] + random(-30, 30); | |
| int duration = durations[i] + random(-40, 40); | |
| buzzer.tone(8, pitch, duration); | |
| digitalWrite(LED_BUILTIN, HIGH); | |
| delay(duration + random(60, 100)); | |
| digitalWrite(LED_BUILTIN, LOW); | |
| buzzer.noTone(8); | |
| } | |
| playWarble(8, NOTE_D4, false); | |
| } | |
| // Quick randomized oscillation for expressive “robotic” finish | |
| void playWarble(int pin, int baseNote, bool upbeat) { | |
| int bursts = random(6, 10); | |
| for (int i = 0; i < bursts; i++) { | |
| int pitch = baseNote + random(-150, 150); | |
| int duration = random(30, 60); | |
| buzzer.tone(pin, pitch, duration); | |
| digitalWrite(LED_BUILTIN, HIGH); | |
| delay(duration / (upbeat ? 1.2 : 0.8)); // Slightly faster if upbeat | |
| digitalWrite(LED_BUILTIN, LOW); | |
| buzzer.noTone(pin); | |
| } | |
| } | |
| void setup() | |
| { | |
| Serial.begin(9600); | |
| randomSeed(analogRead(0)); // Initialize random seed | |
| pinMode(LED_BUILTIN, OUTPUT); | |
| } | |
| float max = 350.00; | |
| float green = 100.00; | |
| float red = 25.00; | |
| void stop() | |
| { | |
| leftMotor.stop(); | |
| rightMotor.stop(); | |
| } | |
| void goForward(float motorSpeed) | |
| { | |
| leftMotor.run(-motorSpeed); | |
| rightMotor.run(motorSpeed); | |
| } | |
| void wideTurn() | |
| { | |
| leftMotor.run(-60); | |
| rightMotor.run(120); | |
| } | |
| void reverse(float motorSpeed) | |
| { | |
| goForward(-motorSpeed); | |
| } | |
| void sharpTurn() | |
| { | |
| reverse(100); | |
| delay(700); | |
| leftMotor.run(100); | |
| rightMotor.run(100); | |
| delay(1000); | |
| } | |
| void loop() | |
| { | |
| float distance = ultraSensor.distanceCm(); | |
| Serial.println(distance); | |
| if (distance < max) { | |
| if (distance >= green) { | |
| goForward(200); | |
| } else if (distance > red) { | |
| wideTurn(); | |
| } else { | |
| stop(); | |
| playSadBeep(); | |
| sharpTurn(); | |
| } | |
| } else { | |
| wideTurn(); | |
| } | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment