Skip to content

Instantly share code, notes, and snippets.

@davidrs
Created February 9, 2017 07:08
Show Gist options
  • Save davidrs/ad89f032e86ae2e28914cd59c54c1294 to your computer and use it in GitHub Desktop.
Save davidrs/ad89f032e86ae2e28914cd59c54c1294 to your computer and use it in GitHub Desktop.
/*
Patrick the drink pourer
This code is for an Arduino hooked up to a servo
that will sweep when it sees a sudden delta in its
photoresistor.
Photoresistor wires: A4, 10~
Resistor A4 to Grnd
Servo: 9~, 5V and Grnd
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int sensorPin = A4; // select the input pin for the potentiometer
int sensorValue = 100; // variable to store the value coming from the sensor
int oldSensorValue = 0;
int pos = 0; // variable to store the servo position
int minDegree = 28;
int maxDegree = 92;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(9600);
}
void loop()
{
sensorValue = (2*sensorValue+analogRead(sensorPin))/3;
// Serial.println(sensorValue-oldSensorValue);
delay(50);
Serial.println(sensorValue-oldSensorValue);
if((sensorValue -oldSensorValue) < -10){
for(pos = maxDegree; pos>=minDegree; pos-=1)
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
// Stay down low for the pour
delay(800);
for(pos = minDegree; pos <= maxDegree; pos += 1)
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
oldSensorValue = sensorValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment