Skip to content

Instantly share code, notes, and snippets.

@0348686
Created February 23, 2026 22:19
Show Gist options
  • Select an option

  • Save 0348686/c360b6f9cb9a17db58d9153733c3e41d to your computer and use it in GitHub Desktop.

Select an option

Save 0348686/c360b6f9cb9a17db58d9153733c3e41d to your computer and use it in GitHub Desktop.
Simon Liesinger -- code for semester 1 project 1 year 11 robotics & mechatronics
// C++ code
//
#include <Servo.h>
long duration;
long dist;
int input;
int timer = 0;
bool locked = false;
bool buzzing = false;
Servo spinner;
int rot = 0;
int highest = 0;
int time_since_highest = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
pinMode(13, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(12, INPUT);
pinMode(A2, INPUT); //sound sensor
pinMode(11, OUTPUT);
for (int i = 2; i < 8; i++) {
pinMode(i, OUTPUT);
}
Serial.begin(9600);
spinner.attach(11);
}
void loop()
{
// if (random(200) == 0) {
// rot += 90;
// }
if (rot > 180) {
rot -= 270;
}
spinner.write(rot);
//read button and increment counter
if (digitalRead(13) == HIGH) {
if (!locked) {
buzzing = !buzzing;
timer += 1;
locked = true;
}
} else {
locked = false;
}
//display timer on the LEDs
for (int i = 2; i < 7; i++) { //by iterating over their pins
if ((timer>>(i-2))%2 == 1) { // and accordingly bitshifting timer
digitalWrite(i, HIGH);
} else {
digitalWrite(i, LOW);
}
}
//control buzzer w/ potentiometer
input = map(analogRead(A0), 0, 1023, 0, 4000); // scale the pot to be within 0-2k hertz.
// Serial.println(input);
if (input < 80 || input > 3960 || !buzzing) { // cut off bad values
noTone(7);
} else {
tone(7, input);
}
int total = 0;
for (int i = 0; i < 3; i++) {
//trigger ultrasonic
digitalWrite(8, LOW);
delayMicroseconds(5);
digitalWrite(8, HIGH);
delayMicroseconds(5);
digitalWrite(8, LOW);
total += pulseIn(12, HIGH);
}
//read ultrasonic
duration = map(total/3, 0, 2000, 100, 0); //2000 is approximately the maximum value of the ultrasonic sensor
duration = duration < 0 ? 0 : duration; //but only approximately.
Serial.println(duration);
if (duration > 40) {
analogWrite(9, 20);
digitalWrite(10, HIGH);
digitalWrite(A1, LOW);
// with more motors this would drive one faster than the other to turn
} else {
analogWrite(9, 20);
digitalWrite(A1, HIGH);
digitalWrite(10, LOW);
// all motors at the same speed
}
analogWrite(9, 255);
total = 0;
for (int i = 0; i < 10; i++) {
total += analogRead(A2);
delay(5);
}
if (total > highest) {
highest = total;
time_since_highest = 0;
} else {
time_since_highest++;
}
if (time_since_highest > 100) {
highest = 0;
}
// Serial.println(highest);
if (total > 1400) {
rot += 90;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment