Upload
vutram
View
214
Download
0
Embed Size (px)
Citation preview
Lecture (02)Arduino 2
By:
Dr. Ahmed ElShafee
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 21
Project 02, press controlled led
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
2
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
3
#define LED 12#define BUTTON 7int Button_status=0;
void setup(){pinMode(LED, OUTPUT);pinMode(BUTTON,INPUT);}
void loop(){Button_status=digitalRead(BUTTON);if(Button_status==HIGH){digitalWrite(LED,LOW);}else{digitalWrite(LED,HIGH);}}
Project 03, press controlled led status toggle
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
4
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
5
#define LED 12#define BUTTON 7int Button_status=0;int Led_Status=0;void setup(){pinMode(LED, OUTPUT);pinMode(BUTTON,INPUT);digitalWrite(LED,Led_Status);}
void loop(){Button_status=digitalRead(BUTTON);if(Button_status==LOW){Led_Status=!Led_Status;digitalWrite(LED,Led_Status);delay(1000);}}
Project 04; press controlled led Flasher•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
6
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
7
#define LED 12#define BUTTON 7int Button_status=0;int Led_Status=0;void setup(){pinMode(LED, OUTPUT);pinMode(BUTTON,INPUT);digitalWrite(LED,Led_Status);}
void loop(){Button_status=digitalRead(BUTTON);if(Button_status==LOW){Led_Status=!Led_Status;digitalWrite(LED,Led_Status);delay(250);}else{Led_Status=0;digitalWrite(LED,Led_Status);}}
Analog Output
• To create an analog signal, the microcontroller uses a technique called PWM. By varying the duty cycle, we can mimic an “average” analog voltage.
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
8
•Pulse Width Modulation (PWM)
Project 05, led dimmer
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
9
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
10
#define LED 3int brightness=0;int fade=5;void setup(){pinMode(LED, OUTPUT);}
void loop(){analogWrite(LED,brightness);brightness+=fade;if((brightness==0) || (brightness==255))fade=‐fade;delay(10);}
Analog input
• Arduino uses a 10‐bit A/D Converter:
• this means that you get input values from 0 to 1023
• 0 V 0
• 5 V 1023
Ex:
• int sensorValue = analogRead(A0);
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
11
Project 06, Analog DC dimmer
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
12
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
13
#define LED 3int brightness=0;int control=0;void setup(){pinMode(LED, OUTPUT); }
void loop(){control=analogRead(0);brightness=control/4;analogWrite(LED,brightness);}
Project 07, serial echo
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
14
void setup(){Serial.begin(9600); }void loop(){if (Serial.available() > 0) {char inByte = Serial.read();Serial.print("you typed : ");Serial.print(inByte);Serial.println();
}
}
Project 8, serial controlled leds
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
15
•
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
16
#define led1 11#define led2 12int led1_status=0;int led2_status=0;void setup(){Serial.begin(9600); pinMode(led1,OUTPUT);pinMode(led2,OUTPUT);digitalWrite(led1,led1_status);digitalWrite(led2,led2_status);}void loop(){if (Serial.available() > 0) {char inByte = Serial.read();switch(inByte){case '1':led1_status=!led1_status;digitalWrite(led1,led1_status);Serial.print("led one is now :"); Serial.println(led1_status);
break;case'2':
led2_status=!led2_status;digitalWrite(led2,led2_status);Serial.print("led two is now :");Serial.println(led2_status); break;default:Serial.println("unknown command"); break;}}}
Project 9, serial controlled leds, monitor press and pot
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
17
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
18
#define led1 11#define led2 12#define button 7int led1_status=0;int led2_status=0;int potValue=0;int pressStatus=1;void setup(){Serial.begin(9600); pinMode(led1,OUTPUT);pinMode(led2,OUTPUT);digitalWrite(led1,led1_status);digitalWrite(led2,led2_status);}void loop(){if(pressStatus!=digitalRead(button)){pressStatus=(int)digitalRead(button);Serial.print("Button value = ");Serial.println(pressStatus);}
if (Serial.available() > 0) {char inByte = Serial.read();switch(inByte){case '1':led1_status=!led1_status;digitalWrite(led1,led1_status);Serial.print("led one is now :"); Serial.println(led1_status);break;case '2':led2_status=!led2_status;digitalWrite(led2,led2_status);Serial.print("led two is now :");Serial.println(led2_status); break;case 'p':potValue=analogRead(0);Serial.print("Pot value = ");
Serial.println((float)potValue*5/1024); break;
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
19
default:Serial.println("unknown command"); break;}}
}
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
20
#define led1 11#define led2 12#define button 7int led1_status=0;int led2_status=0;int potValue=0;int pressStatus=1;void setup(){Serial.begin(9600); pinMode(led1,OUTPUT);pinMode(led2,OUTPUT);digitalWrite(led1,led1_status);digitalWrite(led2,led2_status);}void loop(){if(analogRead(0)!=potValue){potValue=analogRead(0);Serial.print("Pot value = ");
Serial.println((float)potValue*5/1024); }
if(pressStatus!=digitalRead(button)){pressStatus=(int)digitalRead(button);Serial.print("Button value = ");Serial.println(pressStatus);}if (Serial.available() > 0) {char inByte = Serial.read();switch(inByte){case '1':led1_status=!led1_status;digitalWrite(led1,led1_status);Serial.print("led one is now :"); Serial.println(led1_status);break;case '2':led2_status=!led2_status;digitalWrite(led2,led2_status);Serial.print("led two is now :");Serial.println(led2_status); break;
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2
21
default:Serial.println("unknown command"); break;}}
}
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2neering 2App EE II
22
Thanks,..
See you next week (ISA),…
Dr. Ahmed ElShafee, ACU : Spring 2019 ‐ EEP02 Practical Applications in Electrical Engineering 2neering 2App EE II
23