Last active
May 15, 2021 04:31
-
-
Save Devwarlt/cf8d430b35e42cdd931154760e461382 to your computer and use it in GitHub Desktop.
Projeto #1 - Sistemas Embarcados
This file contains 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
/* | |
TinkerCAD -> https://www.tinkercad.com/things/2GZe8DThGtU | |
Embedded Systems - UniProjeção | |
Professor: João Evangelista | |
Student: Nádio Dib | |
# About: | |
This is an abstract simulator of what dynamo does | |
to energyze a whole circuit by "movement", but in | |
this case it'll be trigged by a button that represents | |
the user input to activate current embedded system | |
attached to this dynamo abstraction. | |
# Components: | |
- 1x polirized capacitor 500uF 25V | |
- 1x green LED | |
- 1x RGBA LED | |
- 4x resistors 220 Ohms | |
- 2x resistor 100 Ohms | |
- 1x resistor 10K Ohms | |
- 1x push button 4 pins | |
- 1x multimeter | |
- 1x breadboard | |
- 1x Arduino UNO R3 | |
- 1x ultrasonic sensor | |
- 1x transistor nMOS (MOSFET) | |
- 1x buzzer | |
*/ | |
// Time constants | |
unsigned int elapsed_ms_delta = 0; | |
unsigned long ticks_count = 0; | |
const unsigned int delay_ms_threshold = 1 * 1000; | |
bool is_next_tick(void) { | |
return (elapsed_ms_delta % delay_ms_threshold) == 0; | |
} | |
// Serial Monitor settings | |
#define SERIAL_NUMBER 9600 | |
// Dynamo settings | |
#define DYNAMO_PIN 2 | |
#define TARGET_PIN 3 | |
bool is_dynamo_btn_pulled(void) | |
{ | |
unsigned char digital_value = digitalRead(DYNAMO_PIN); | |
return digital_value == HIGH; | |
} | |
// RGBA LED settings | |
#define RED_LED_PIN 6 | |
#define GREEN_LED_PIN 4 | |
#define BLUE_LED_PIN 5 | |
void set_rgb_color( | |
unsigned char r, | |
unsigned char g, | |
unsigned char b) { | |
digitalWrite(RED_LED_PIN, (int)r); | |
digitalWrite(GREEN_LED_PIN, (int)g); | |
digitalWrite(BLUE_LED_PIN, (int)b); | |
} | |
// Ultrasonic Sensor settings | |
#define SPEED_OF_SOUND 286.75F | |
#define DISTANCE_THRESHOLD 0.70F | |
#define USENSOR_SIGNAL_PIN 7 | |
float get_distance(void) { | |
// The main reason why the ultrasonic sensor pin | |
// isn't configured at method 'setup()' is because | |
// when we are processing speed of sound when are | |
// sending a sound wave to be collided with something | |
// and then we are awaiting for an incoming reflection | |
// of that same wave. In other words, we are sending | |
// (OUTPUT) and then receiving (INPUT) sound waves. | |
// Another important topic is, when we are calculating | |
// a relative distance between target and sensor | |
// position, its required to divide by 2 the measure. | |
pinMode(USENSOR_SIGNAL_PIN, OUTPUT); | |
digitalWrite(USENSOR_SIGNAL_PIN, LOW); | |
delayMicroseconds(2); | |
digitalWrite(USENSOR_SIGNAL_PIN, HIGH); | |
delayMicroseconds(2); | |
digitalWrite(USENSOR_SIGNAL_PIN, LOW); | |
pinMode(USENSOR_SIGNAL_PIN, INPUT); | |
float elapsed_us = pulseIn(USENSOR_SIGNAL_PIN, HIGH); | |
float distance_meters = elapsed_us / SPEED_OF_SOUND; | |
distance_meters /= 20; | |
Serial.print("[Tick: "); | |
Serial.print(ticks_count); | |
Serial.print("] "); | |
Serial.print("Distance: "); | |
Serial.print(distance_meters); | |
Serial.println(" m"); | |
return distance_meters; | |
} | |
// Buzzer settings | |
#define BUZZER_MAX_FREQUENCY 1000.0F | |
#define BUZZER_PIN 8 | |
void play_buzzer(void) { | |
for (int i = BUZZER_MAX_FREQUENCY; i > 0; i--) { | |
if (i % 50 == 0) | |
tone(BUZZER_PIN, i); | |
delay(1); | |
} | |
} | |
void setup(void) | |
{ | |
// Serial Monitor | |
Serial.begin(SERIAL_NUMBER); | |
// Dynamo pins | |
pinMode(DYNAMO_PIN, INPUT); | |
pinMode(TARGET_PIN, OUTPUT); | |
// RGBA LED pins | |
pinMode(RED_LED_PIN, OUTPUT); | |
pinMode(GREEN_LED_PIN, OUTPUT); | |
pinMode(BLUE_LED_PIN, OUTPUT); | |
// Buzzer pin | |
pinMode(BUZZER_PIN, OUTPUT); | |
} | |
void loop(void) | |
{ | |
// Represents the time core of this application used | |
// for time measurement to avoid log spamming. | |
elapsed_ms_delta++; | |
// If button is pulled then it'll energyze whole | |
// circuit through Arduino pins. So, this is pretty | |
// much a simulator of a power supply by "movement" | |
// AKA what dynamos does in other words. Therefore, | |
// that polarized capacitor leak energy over time and | |
// it to keep the embedded system alive is when the | |
// button is pulled. | |
bool is_pulled = is_dynamo_btn_pulled(); | |
digitalWrite(TARGET_PIN, is_pulled ? HIGH : LOW); | |
// Let user know when embedded system is deactivated. | |
if (!is_pulled) { | |
if (is_next_tick()) { | |
Serial.print("[Tick: "); | |
Serial.print(ticks_count); | |
Serial.print("] "); | |
Serial.println("Circuit isn't energyzed."); | |
} | |
set_rgb_color(HIGH, HIGH, LOW); | |
noTone(BUZZER_PIN); | |
} | |
else { | |
// Based on distance between sensor and object | |
// it'll play buzzer + change RGBA LED color. | |
float distance = get_distance(); | |
if (distance > DISTANCE_THRESHOLD) { | |
set_rgb_color(LOW, HIGH, LOW); | |
noTone(BUZZER_PIN); | |
} | |
else { | |
set_rgb_color(HIGH, LOW, LOW); | |
play_buzzer(); | |
} | |
} | |
// Reset when needed the elapsed milliseconds delta. | |
if (is_next_tick()) { | |
elapsed_ms_delta = 0; | |
ticks_count++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment