A Salute to Arduino

saluteFor this project I used a flex sensor attached to the inside of my elbow to tell a servo motor where to point. When my arm is down, it commands me to salute. When I salute, it tells me to be at ease.  You can watch the video but it’s essentially the same as the gif.

//control a servo position using a flex sensor

#include <Servo.h> //import servo library

Servo myservo;  //create servo object to control a servo

int flexpin = 0;  //flex sensor connected to analog pin 0
int val; //variable for reading analog pin

void setup() {
  Serial.begin(9600);
  myservo.attach(9); //attaches the servo to pin 9
}

void loop() {
  val = analogRead(flexpin);  //read value of flex sensor (between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);  //scale it to use with servo (0 and 180 degrees)
  Serial.println(val); //print the scaled read from the analog pin
  myservo.write(val);  //set the servo position according to the scaled value
  delay(15);   //wait for the servo to move
}

 

Leave a Reply