Last active
August 29, 2015 14:02
-
-
Save carlok/3bbe3ba1fe12e4618c12 to your computer and use it in GitHub Desktop.
Arduino Light (IN) / ServoMotor (OUT)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Arduino Light (IN) / ServoMotor (OUT) | |
When light intensity is above 500, every second the engine steps 5° | |
clockwise: it steps anticlockwise the other way round... we also measure the | |
temperature but it is not used at the moment. | |
Video: http://youtu.be/77J9FhmZNK8 | |
(C) 2014 by Carlo Perassi - http://perassi.org/2014/06/19/arduino-light-in-servomotor-out - GPLv3 | |
*/ | |
#include <Servo.h> | |
const int ledPin = 13; | |
const int servoPin = 8; | |
const int sensorTempPin = A0; | |
const int sensorLightPin = A1; | |
int sensorLightValue; | |
int sensorLightLow = 1023; | |
int sensorLightHigh = 0; | |
Servo mServo; | |
int servoAngle = 0; | |
void setup() { | |
Serial.begin(9600); | |
setupLight(); | |
setupServo(); | |
} | |
void setupLight() { | |
pinMode(ledPin, OUTPUT); | |
digitalWrite(ledPin, HIGH); | |
while(millis() < 5000) { | |
sensorLightValue = analogRead(sensorLightPin); | |
if (sensorLightValue > sensorLightHigh) { | |
sensorLightHigh = sensorLightValue; | |
} | |
if (sensorLightValue < sensorLightLow) { | |
sensorLightLow = sensorLightValue; | |
} | |
} | |
digitalWrite(ledPin, LOW); | |
} | |
void setupServo() { | |
mServo.attach(servoPin); | |
} | |
void lightPrint(int sensorVal) { | |
sensorLightValue = sensorVal; | |
Serial.print("Light => "); | |
Serial.println(sensorLightValue); | |
} | |
void servoSpin() { | |
if (sensorLightValue > 500) { | |
servoAngle = servoAngle == 175 ? 0 : servoAngle + 5; | |
} else { | |
servoAngle = servoAngle == 0 ? 175 : servoAngle - 5; | |
} | |
mServo.write(servoAngle); | |
} | |
void temperaturePrint(int sensorVal) { | |
float voltage = (sensorVal / 1024.0) * 5.0; | |
float temperature = (voltage - .5) * 100; | |
Serial.print("Temperature => "); | |
Serial.println(temperature); | |
} | |
void loop() { | |
lightPrint(analogRead(sensorLightPin)); | |
temperaturePrint(analogRead(sensorTempPin)); | |
Serial.println(); | |
servoSpin(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment