Skip to content

Instantly share code, notes, and snippets.

@lizkhoo
Created October 8, 2012 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizkhoo/3855399 to your computer and use it in GitHub Desktop.
Save lizkhoo/3855399 to your computer and use it in GitHub Desktop.
butter my bread
//butter machine for Arduino
#include <Servo.h> //add servo motor library
const int photoPin = A0; //input pin for photosensor
const int ledPin = 9;
int photoMin = 1023;
int photoMax = 0;
int photoValue = 1023;
Servo servoMotor;
const int servoPin = 2;
int servoAngle[]={
30,149};
void setup(){
Serial.begin(9600);
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
while (millis() < 2000){
photoValue = analogRead(photoPin);
if (photoValue > photoMax){
photoMax = photoValue;
Serial.print("photoMax = ");
Serial.println(photoMax);
}
if (photoValue < photoMin){
photoMin = photoValue;
Serial.print("photoMin = ");
Serial.println(photoMin);
}
}
digitalWrite(9,LOW);
servoMotor.attach(servoPin);
}
void loop(){
photoValue = analogRead(photoPin);
photoValue = map(photoValue, photoMin, photoMax, 0, 179);
photoValue = constrain(photoValue,0,179);
Serial.print("photoValue = ");
Serial.println(photoValue);
for(int i = 0; i < 2; i++){
if (photoValue < 10 && millis() > 2000){
digitalWrite(9,HIGH);
servoMotor.write(servoAngle[i]);
Serial.print("servoAngle = ");
Serial.println(servoAngle[i]);
delay(1000);
} else {
digitalWrite(9,LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment