In this improvement, we used yellow, green, red leds, 2 push buttons and 1 servo. This mini project is a combination of 3 different hardware components. Our task is by looking the state of push buttons, changing the servo state. If push button 1 is low, we increase the servo state. If push button 2 is low, we decrease the servo state. Our 2nd task is, by looking this servo state we changed the states of these 3 leds. We did this by a simple way; using if else statements.
Berrak Şişman, İlker Çam
Our circuit photo is given below.
#include "servo.h" const int LedRed = 13; const int LedGreen = 11; const int LedYellow = 12; const int btn1 = 2; const int btn2 = 3; const int servoPin = 9; Servo servo1; unsigned int ServoPosition = 90; void setup() { // put your setup code here, to run once: servo1.attach(servoPin); pinMode(btn1,INPUT); pinMode(btn2,INPUT); pinMode(LedRed,OUTPUT); pinMode(LedGreen,OUTPUT); pinMode(LedYellow,OUTPUT); servo1.write(ServoPosition); Serial.begin(9600); } void loop() { int btn1State,btn2State; unsigned int servoStateHasBeenChanged = 0; btn1State = digitalRead(btn1); btn2State = digitalRead(btn2); Serial.println(ServoPosition); if(btn1State == LOW){ ServoPosition = ((ServoPosition + 4 )%180); servoStateHasBeenChanged = 1; Serial.println("BTN 1 Pressed\n"); }else if(btn2State == LOW){ ServoPosition = ((ServoPosition - 4 )%180); servoStateHasBeenChanged = 1; Serial.println("BTN 2 Pressed\n"); } if(ServoPosition<92 data-blogger-escaped-servoposition=""> 88){ digitalWrite(LedGreen,LOW); digitalWrite(LedRed,LOW); digitalWrite(LedYellow,HIGH); }else if(ServoPosition<90 data-blogger-escaped-digitalwrite="" data-blogger-escaped-edgreen="" data-blogger-escaped-edred="" data-blogger-escaped-edyellow="" data-blogger-escaped-else="" data-blogger-escaped-ervoposition="" data-blogger-escaped-if="">90){ digitalWrite(LedGreen,LOW); digitalWrite(LedRed,HIGH); digitalWrite(LedYellow,LOW); } if(servoStateHasBeenChanged == 1){ servo1.write(ServoPosition); } delay(50); }
No comments:
Post a Comment