Lab 3
LED and servoTry from gabriella levine on Vimeo.
In this video, I use a flexor sensor to turn on a servo motor. However, I’m unsuccessful in trying to get an LED light to light up also.
And here’s the breadboard. See the code on the next page.
Flexor Sensor from gabriella levine on Vimeo.
And above, I use a flexor sensor to turn on and off an LED.
PCOMP lab 3 potentiometer from gabriella levine on Vimeo.
Turn the potentiometer up and the LED light gets brighter.
Just a note: At first I had digital output from pin 13 but since it’s not PWM , the potentiometer was acting more like a switch than an analog reader. So I switched it to pin 9, as the lab designated.
Code for Servo Motor and LED with flexor sensor (unsuccessful)
#include <Servo.h> // include the servo library
Servo servoMotor; // creates an instance of the servo object to control a servo
int led = 10; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int analogPin = 0; // the analog pin that the sensor is on
int analogValue = 0; // the value returned from the analog sensor
int potPin = 0;
int sensorValue = 0;
int servoPin = 2; // Control pin for servo motor. As of Arduino 0017, can be any pin
void setup() {
servoMotor.attach(servoPin); // attaches the servo on pin 2 to the servo object
Serial.begin(9600);
// declare the led pin as an output:
pinMode(led, OUTPUT);
}
void loop()
{ sensorValue = analogRead(potPin);
analogValue = analogRead(analogPin); // read the analog input (value between 0 and 1023)
analogValue = map(analogValue, 0, 1023, 0, 179); // map the analog value (0 – 1023) to the angle of the servo (0 – 179)
servoMotor.write(analogValue); // write the new mapped analog value to set the position of the servo
delay(15); // waits for the servo to get there
int brightness = map(analogValue, 250,300, 255,0);
analogWrite(led, brightness); // set the LED brightness with the result
Serial.println(analogValue); // print the pot value back to the debugger pane
}
One Response to “Lab 3”
[…] I rearranged the code from before and was able to get the LED to blink and the motor to spin when the flex sensor was touched. See […]