Day 9
For today we had to create a program that lit up certain LEDs based on the distance measured by the PIR sensor Here's the code #include <Servo.h> const int SERVO =9; const int IR =0; const int LED1 = 3; const int LED2 = 5; const int LED3 =6; const int LED4= 11; Servo myServo; int dist1 =0; int dist2=0; int dist3=0; int dist4=0; void setup() { myServo.attach(SERVO); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(LED4, OUTPUT); } int readDistance(int pos) { myServo.write(pos); delay(600); int dist= analogRead(IR); dist= map(dist,50,500,0,255); dist =constrain(dist,0,255); return dist; } void loop() { dist1 = readDistance(15); analogWrite(LED1,dist1); delay(300); dist2=readDistance(65); analogWrite(LED2,dist2); delay(300); dist3=readDi...




Comments
Post a Comment