/* EEL-5666 Example 4: Use of Digital Output Pins Turn on/off External LEDs connected to Pins 11 & 12 alternatively and the internal LED on Pin 13 on/off. Add a delay in each state of 1/2 sec. Use serial input to control the LEDs. {i,o} for internal LED {j,k} for external LEDs. Read Cds cell on Analog A0 and control the internal LED. Use pin 9 to sweep a servo. Arroyo January 21, 2016 */ #include Servo myservo; // create servo object to control a servo int extLED1 = 11; // External LED connected to digital pin 11 int extLED2 = 12; // External LED connected to digital pin 12 int intLED = 13; // Internal LED connected to digital pin 13 void setup( ) { pinMode(extLED1, OUTPUT); // sets digital pin 12 as output pinMode(extLED2, OUTPUT); // sets digital pin 12 as output pinMode(intLED, OUTPUT); // sets digital pin 13 as output Serial.begin(57600); // connect to the serial port myservo.attach(9); // attaches servo to pin 9 } int rcvData=0, count=0, j=0, a1, ambient; void loop( ) { if (count < 1) { Serial.println("Arduino MEGA2560 Example"); Serial.println("Cds Cell connected on Analog A0"); Serial.println("Control the internal LED on pin 13"); Serial.println("Servo Connected on Pin9"); ambient = analogRead(A0); Serial.print("Ambient: "); Serial.println(ambient); for (j=0; j<5; j++){ Serial.print("j= "); Serial.println(j); digitalWrite(13, HIGH); // set the internal LED on digitalWrite(12, LOW); // set the external LED1 off digitalWrite(11, HIGH); // set the external LED2 on delay(500); // wait for a 1/2 second digitalWrite(13, LOW); // set the internal LED off digitalWrite(12, HIGH); // set the external LED on digitalWrite(11, LOW); // set the external LED off delay(500); // wait for a 1/2 second } digitalWrite(13, HIGH); // set the internal LED on digitalWrite(12, HIGH); // set the external LED1 on digitalWrite(11, HIGH); // set the external LED2 on } count=1; // Sweep Servo for(j = 0; j < 180; j += 10) { // goes from 0 degrees to 180 degrees myservo.write(j); // tell servo to go to position in variable 'pos' Serial.print("Ambient: "); Serial.print(ambient); Serial.print(" A1: "); a1 = analogRead(A0); if (a1 > 1.1*ambient ) digitalWrite(13, LOW); // turn off DBLED else digitalWrite(13, HIGH); // else turn on DBLED Serial.println(a1); delay(100); // waits 15ms for the servo to reach the position } for(j = 180; j >= 1; j -= 10) { // goes from 180 degrees to 0 degrees myservo.write(j); // tell servo to go to position in variable 'pos' Serial.print("Ambient: "); Serial.print(ambient); Serial.print(" A1: "); a1 = analogRead(A0); if (a1 > 1.1*ambient ) digitalWrite(13, LOW); // turn off DBLED else digitalWrite(13, HIGH); // else turn on DBLED Serial.println(a1); delay(100); // waits 15ms for the servo to reach the position } // blink data only when you receive data: if (Serial.available() > 0) { // read the incoming byte: rcvData = Serial.read(); // say what you got: Serial.print("I received: "); Serial.write(rcvData); Serial.println(); if (rcvData == 'j') {digitalWrite(12,LOW); digitalWrite(11,HIGH);} else if (rcvData == 'k') {digitalWrite(12,HIGH); digitalWrite(11,LOW);} // else if (rcvData == 'i') digitalWrite(13, HIGH); // internal LED on // else if (rcvData == 'o') digitalWrite(13, LOW); // internal LED off } }