Task description as mentioned on the portal: design and program a smart device that performs a certain function or solves a problem: program Arduino UNO to read signals from multiple input components (Sensor, Switch, or variable resistor) to control multiple action components (Motor, Buzzer, LED...etc) using Arduino C (Text Code).
Ideation: I would like to build a smart alarm that looks like a mini figure with a hat and two LEDs that mimic his eyes.
In his hat, there is a light sensor that sends a signal to the LEDs when he wears the hat at night so they glow with calming light during sleep time. When he takes the hat off in the morning, the LEDs will go to a low mode and a servo motor will move his hands 45 degrees to great the user and wish him/her a good day!
inspiration photo of Pinocchio marionette
Thinkercad to simulate circuit and test the code
components used to build the circuit
A corrugated cardboard sheet on BRM Laser machine
Arduino IDE to write the code and test the function
LaseWorks software to cut the opensource design
Cutting tools and double tape for the assembly
program interface when a new sketch is open on Arduino IDE
following video tutorial about LED Double Blinking
Unlike last week when we used the code blocks, this week we will learn how to write the code from scratch on Arduino IDE. When we open a new sketch from the file menu, two commands will be written already on the program interface:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Between the curly bracket, this is where we write our code. And what is written between the double slashes is a general comment that does not have any format just to remind myself of any note or description I want to associate with the code below.
The shortcut for auto-format (Ctrl+t) is used for a better alignment of the code and to spot errors easily.
"void setup" section is for identifying the pins and labeling them as an output or an input as this function needs to be communicated once to the program.
"void loop" will include commands that are more likely to be repetitive functions.
Understanding Arduino syntax:
Arduino is case sensitive and small letters have to remain small, and capital letters have to remain capital.
How we store data on Arduino depends on the type of data:
int x = analogRead(9); //to store interval/real number//
long y = analogWrite(A0); //extended size variables for number storage//
float z = y/10 //to store decimal digits//
char letter = "e"; //to store letters//
Components are defined based on two parameters: their pin number and if they are considered to be an input/output.
When we associate the LED color with the pin number, our code will be more readable, and if I need to change wiring we just need to change the PIN number, not all code:
Int red = 8;
Serial Monitor is a communication method to transfer data between devices which in this case the laptop and Arduino and the USB (universal serial bus) is the link between them. We use this feature to display output and track values, and also to create wireless communication between devices using the Bluetooth module. What should be added to the void setup code is to notify Arduino to activate this feature:
Serial.begin(9600);
and to void loop some of these functions.
Serial.print("Temp = ");
or/and
Serial.println();
It took me a while to understand to write variables to control an RGB LED using a potentiometer that uses an analog pin with range of different value numbers:
void loop() {
int sensor_reading = analogRead(A0);
int interval = map(analogRead(A0), 0, 1023, 100, 2000);
Creating a Loop using If conditions: LED Fading
Creating a Loop using For Loop: LED Fading
Controlling an RGB LED with an Ultrasonic Sensor
What does it mean by looping?
Looping is about repeating functions endlessly or until another condition interrupts this loop.
Using the main loop "void loop" to run one function like LED fading is not ideal in case of the need of adding more functions.
Example for such a code: from file -> example -> basics -> Fading:
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
Creating a loop function using For Loop will give room for another function to be added and remain repetitive as well outside of the For loop but in the main loop.
Example for such a code: from file -> example -> analog -> Fading:
void loop() {
for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
When adding conditions related to the distance between the Ultrasonic sensor and an object, and based on these conditions the LED should blink or fade, the Arduino scan conditions according to their orders in the code and by adding "else" at the end; I am communicating to the Arduino if you could not find non of the above, run this condition and in the cause of this code, the LED bulb will remain glow.
Digging into the task of this week:
I wanted to use the data from the light sensor to control two LEDs and one servo motor. I started by linking the sketch with the servo library and defining each pin with a name so that when I change the wiring, I will simply change the number of the pin at the beginning of the code:
#include <Servo.h>
int POS = 90;
int Sensor = A0;
int LED1 = 6;
int LED2 = 7;
int onbutton = 5;
Servo A;
Then in the void setup section, I defined every pin as input/output and the servo pin. I also added one function that I want it to run once only at the beginning of the code which is servo goes to its initial position.
void setup() {
A.attach(9);
A.write(POS);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(Sensor, INPUT);
pinMode(onbutton, INPUT_PULLUP); // Set the onbutton pin as input
}
In the void loop section, I added the if condition which states when the sensor is uncovered/on, the servo motor will move and rotates the arm as if the robot is waving hands saying "good morning" to the user, and LEDs will be turned off.
And if the sensor is covered/off, the LEDs will glow to give the user calming environment at night time, and the motor will remain still.
void loop() {
if (digitalRead(onbutton) == HIGH) {
if (analogRead(Sensor)>800) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
A.write(POS);
} else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
A.write(180);
delay(500);
A.write(0);
delay(500);
A.write(180);
delay(500);
A.write(0);
}
I cut the design with 30 power and 50 speed on the BRM Laser machine, and for the speed cut, I used 250 speed and 12 power.
had hard time fitting the wires inside of the object
fixing the servo on the inner side of the model
when the sensor is uncovered and the hand waves with the motor (morning time)
when the sensor is covered and the LEDs glow in the dark (night time)
adding ON/OFF switch component as a second input
The second challenge was related to creating a cardboard arm that moves 90 degrees using the servo motor.
Below is a brief explanation of the steps I followed to solve such a problem which was related to the length of the arm and how this affected the hand movement.
had to change the ON/OFF switch with this one
I had three challenges finishing this task, besides the coding part:
One of the requirements of this task is to include two inputs, and I only included the sensor in the first submission of this task. As the concept was already there, and adding another sensor will be irrelevant to the idea, I decided to include an ON/OFF switch and power the device with a 9-volt adaptor.
Another challenge that I had to replace the button with another one as some of the components appeared to have power even when the button was turned off which was an indication that the switch does not cut off power when needed.
The first design of the arm was really short in comparison to the body size.
I had to cut a longer version, and including a curved side to move.
It was the perfect fit with the servo motor and it moves really smoothly.
final model with electronics components inside
I think I have to consider where I will put the wires, and how I will mount other components like the boards and motor.
I have to admit that this week's model needed some adjustments and more planning for a better fit of the components.
the final model in use (morning/ night time)
as broken as me when I saw it
There are different types of mistakes and one of them happens due to lack of sleep.
To make a long story short, I had to cut the model twice.