Lecture (02) Arduino2 By: Dr. Ahmed...

12
Lecture (02) Arduino 2 By: Dr. Ahmed ElShafee Dr. Ahmed ElShafee, ACU : Spring 2019 EEP02 Practical Applications in Electrical Engineering 2 1 Project 02, press controlled led Dr. Ahmed ElShafee, ACU : Spring 2019 EEP02 Practical Applications in Electrical Engineering 2 2

Transcript of Lecture (02) Arduino2 By: Dr. Ahmed...

Page 1: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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

Page 2: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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

Page 3: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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

Page 4: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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)

Page 5: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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);}

Page 6: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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

Page 7: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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();

}

}

Page 8: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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;}}}

Page 9: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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;

Page 10: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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;

Page 11: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

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

Page 12: Lecture (02) Arduino2 By: Dr. Ahmed ElShafeedraelshafee.net/Spring2019/eep-practical-applications-in...Dr. Ahmed ElShafee, ACU : Spring 2019 ‐EEP02 Practical Applications in Electrical

Thanks,..

See you next week (ISA),…

Dr. Ahmed ElShafee, ACU : Spring  2019 ‐ EEP02 Practical Applications in Electrical Engineering 2neering 2App EE II 

23