CLASSWORK/ HOMEWORK:
Sweeping Servo 2.0
//Sweeping Distance Sensor
#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;
int LEDonTime =50;
int ServoSpeed =100;
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);
}
void loop()
{
//Sweep the Servo into 4 regions and change the LEDs
dist1 = readDistance(78);
analogWrite(LED1, dist1);
delay(LEDonTime);
dist2 = readDistance(82);
analogWrite(LED2, dist2);
delay(LEDonTime);
dist3 = readDistance(86);
analogWrite(LED3, dist3);
delay(LEDonTime);
dist4 = readDistance(90);
analogWrite(LED4, dist4);
delay(LEDonTime);
}
int readDistance(int pos)
{
myServo.write(pos);
delay(ServoSpeed);
int dist = analogRead(IR);
dist = map(dist, 50, 500, 0, 255);
dist = constrain(dist, 0, 255);
return dist;
}
No comments:
Post a Comment