Noor Alul
The first exercise:
In this exercise, we got acquainted with the basics of the code, each of its pieces, and what it is used for. Then each person chose a song and wrote a special code for the song, whose tones were reflected through the lights, its playback, and its speed. I chose an Arabic song by singer Mohamed Hamaki, called (Ma Balash).
The code for the song (Ma Balash):
//Define variables
//LED pins connected to:
const int Red_Led_Pin=3;
const int Yellow_Led_Pin=4;
const int Green_Led_Pin=5;
const int button_pin=7;
const int poten_pin= A0;
//define variable to hold value from potentiometer
int poten_val;
// define a boolean flag
bool button_state= false;
//function defenitions
void setup() {
//Define the led pins as output so that they can light:
pinMode(Red_Led_Pin, OUTPUT);
pinMode(Yellow_Led_Pin, OUTPUT);
pinMode(Green_Led_Pin, OUTPUT);
//define the button pin as pullup so it can act as a button:
pinMode(button_pin,INPUT_PULLUP);
//OPEN SERIAL COMMUNICATION WITH THE COMPUTER, SO WE CAN SEE THE POTENTIOMETERS
Serial.begin(9600);
}
//Runing codes
void loop() {
// put your main code here, to run repeatedly:
poten_val= analogRead(poten_pin);
Serial.println(poten_val);
delay(500);
Serial.println(digitalRead(button_pin));
delay(100);
if(digitalRead(button_pin)==0){
button_state = !button_state;
}
if(button_state== true)
{
poten_val=analogRead(poten_val);
if (poten_val>=682)
{
digitalWrite(Red_Led_Pin,HIGH);
digitalWrite(Yellow_Led_Pin,LOW);
digitalWrite(Green_Led_Pin,LOW);
}
else if(poten_val>=341)
{
digitalWrite(Red_Led_Pin,HIGH);
digitalWrite(Yellow_Led_Pin,HIGH);
digitalWrite(Green_Led_Pin,LOW);
}
else{
digitalWrite(Red_Led_Pin,HIGH);
digitalWrite(Yellow_Led_Pin,HIGH);
digitalWrite(Green_Led_Pin,HIGH);
}
delay(100);
}
else{
digitalWrite(Red_Led_Pin,LOW);
digitalWrite(Yellow_Led_Pin,LOW);
digitalWrite(Green_Led_Pin,LOW);
}
The second exercise:
In this exercise, I chose to do something that measures the distance. This was as a first step, and then I wanted to add more. I added a light and an alarm device, where when approaching it, the light lights up and an alarm sounds. The idea was successful and fun.
// defines pins numbers
const int Noor = 9;
const int Angel = 10;
const int Star = 11;
const int Done = 13;
// defines variables
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(Noor, OUTPUT); // Sets the Noor as an Output
pinMode(Angel, INPUT); // Sets the Angel as an Input
pinMode(Star, OUTPUT);
pinMode(Done, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the Noor
digitalWrite(Noor, LOW);
delayMicroseconds(2);
// Sets the Noor on HIGH state for 10 micro seconds
digitalWrite(Noor, HIGH);
delayMicroseconds(10);
digitalWrite(Noor, LOW);
// Reads the Angel, returns the sound wave travel time in microseconds
duration = pulseIn(Angel, HIGH);
// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(Star, HIGH);
digitalWrite(Done, HIGH);
}
else{
digitalWrite(Star, LOW);
digitalWrite(Done, LOW);
}
delay(400);
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}


