Skip to content

Instantly share code, notes, and snippets.

@ahmedlahrizi
Created August 31, 2021 16:49
Show Gist options
  • Save ahmedlahrizi/52bbe6c5826781aa152e894e319c3b8b to your computer and use it in GitHub Desktop.
Save ahmedlahrizi/52bbe6c5826781aa152e894e319c3b8b to your computer and use it in GitHub Desktop.
Arduino big projects
// include the Servo library
#include <Servo.h>
Servo ServoPen; // create a servo object
unsigned short sensors_enabled = 0;
const unsigned short touch_sensor_pin = 10;
const unsigned short servo_pen_pin = 9;
const unsigned short ball_pin = 2;
const unsigned short light_block_pin = 5;
const unsigned short sound_pin = 11;
const unsigned short magnet_pin = 8;
const unsigned short laser_pin = 7;
boolean ball_on = false;
boolean light_block_on = false;
boolean sound_on = false;
boolean magnet_on = false;
boolean ball_default = false;
boolean light_block_default = false;
boolean sound_default = true;
boolean magnet_default = true;
const unsigned short green_led_pin = 3;
const unsigned short yellow_led_pin = 6;
const unsigned short red_led_pin = 12;
const unsigned short minimum_degrees = 60;
const unsigned short maximum_degrees = 135;
const unsigned short margin = 5;
unsigned short touch_sensor_value;
unsigned short servo_pen_value = minimum_degrees;
boolean machine_unlocked = false;
void setupServoPen() {
ServoPen.attach(servo_pen_pin);
ServoPen.write(minimum_degrees); // Initalise Servo to the minimum degrees that let's it write on a surface
delay(200); // Wait for the Servo to move
}
// Script that makes the servo pen work
void useServoPen() {
touch_sensor_value = digitalRead(touch_sensor_pin);
// Checks Weather to increment or decrement the servo pen value
if (touch_sensor_value && servo_pen_value == maximum_degrees) {} // should fix infinite 135-140-135 bug
else if (touch_sensor_value && servo_pen_value <= maximum_degrees)
servo_pen_value += margin;
else if (servo_pen_value > minimum_degrees)
servo_pen_value -= margin;
ServoPen.write(servo_pen_value);
Serial.print("Touch sensor value: ");
Serial.print(touch_sensor_value);
Serial.print(" Servo pen value: ");
Serial.println(servo_pen_value);
}
void refresh_unlock_leds() {
if (machine_unlocked) {
digitalWrite(green_led_pin, HIGH);
digitalWrite(red_led_pin, LOW);
} else {
digitalWrite(red_led_pin, HIGH);
digitalWrite(green_led_pin, LOW);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(laser_pin, OUTPUT);
pinMode(green_led_pin, OUTPUT);
pinMode(yellow_led_pin, OUTPUT);
pinMode(red_led_pin, OUTPUT);
pinMode(ball_pin, INPUT);
pinMode(touch_sensor_pin, INPUT);
pinMode(light_block_pin, INPUT);
pinMode(sound_pin, INPUT);
pinMode(magnet_pin, INPUT);
digitalWrite(laser_pin, HIGH);
}
void monitor_sensors(unsigned short sensor) { // for debug/dev (uses laser)
if (digitalRead(sensor)) {
digitalWrite(yellow_led_pin, HIGH);
} else {
digitalWrite(yellow_led_pin, LOW);
}
Serial.println(digitalRead(sensor));
}
void flash_yellow_led() {
digitalWrite(yellow_led_pin, HIGH);
delay(75);
digitalWrite(yellow_led_pin, LOW);
}
void refresh_sensor_states() {
if (!digitalRead(sound_pin)) {
if (!sound_on) {
flash_yellow_led();
++sensors_enabled;
Serial.println("sound");
}
sound_on = true;
}
if (!digitalRead(magnet_pin)) {
if (!magnet_on) {
flash_yellow_led();
++sensors_enabled;
Serial.println("magnet");
}
magnet_on = true;
}
if (digitalRead(ball_pin)) {
if (!ball_on) {
flash_yellow_led();
++sensors_enabled;
Serial.println("ball");
}
ball_on = true;
}
if (digitalRead(light_block_pin)) {
if (!light_block_on) {
flash_yellow_led();
++sensors_enabled;
Serial.println("light block");
}
light_block_on = true;
}
if (sensors_enabled == 4) {
machine_unlocked = true;
}
Serial.println(sensors_enabled);
}
void loop() {
// put your main code here, to run repeatedly:
while (!machine_unlocked) {
refresh_sensor_states();
if (sensors_enabled >= 1) {
digitalWrite(laser_pin, LOW);
digitalWrite(red_led_pin, HIGH);
}
}
digitalWrite(red_led_pin, LOW);
digitalWrite(green_led_pin, HIGH);
setupServoPen();
while(true) {
useServoPen();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment