Skip to content

Instantly share code, notes, and snippets.

@calebhaye
Last active August 29, 2015 13:59
Show Gist options
  • Save calebhaye/10560419 to your computer and use it in GitHub Desktop.
Save calebhaye/10560419 to your computer and use it in GitHub Desktop.
Arduino Garden
/* Pin Locations */
const int pinLightSensor = A1; // light sensor (analog 1)
const int pinThermistor = A2; // thermistor (analog 2)
const int pinRelayLED = 2; // relay LED (digital 2)
const int pinRelay = 3; // relay (digital 3)
const int pinMotionSensor = 5; // motion sensor (digital 5)
const int pinRangeSensor = 7; // range sensor (digital 7)
const int pinLED = 13; // LED (digital 13)
/* Globally scoped variables */
int thermistorSensorValue; // thermistor
int lightSensorValue; // light sensor
int motionSensorValue; // motion sensor
long rangeSensorDuration, rangeSensorInches, rangeSensorCentimeters; // range sensor
// constructor
void setup()
{
/* I/O */
//open serial port
Serial.begin(9600);
/* OUTPUTS */
// onboard LED
pinMode(pinLED, OUTPUT);
// pinRelay
pinMode (pinRelay, OUTPUT);
// pinRelay LED
pinMode (pinRelayLED, OUTPUT);
/* INPUTS */
// motion sensor
pinMode (pinMotionSensor, INPUT);
/* INIT */
// close the pinRelay
closeRelay();
}
// runtime loop
void loop()
{
// capture motion sensor value
motionSensorValue = digitalRead(pinMotionSensor);
Serial.print("motion: ");
Serial.print(motionSensorValue);
// capture photosensor value
lightSensorValue = analogRead(pinLightSensor);
onLightSensorEvent(lightSensorValue);
// ping range finder
// will capture values for: rangeSensorDuration, rangeSensorInches, rangeSensorCentimeters
rangePing();
// capture thermistor value
thermistorSensorValue = analogRead(pinThermistor);
onThermistorEvent(thermistorSensorValue);
// print new line for serial output
Serial.println();
// chill, iterate
delay(500);
}
void rangePing()
{
// establish variables for rangeSensorDuration of the ping,
// and the distance result in inches and centimeters:
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pinRangeSensor, OUTPUT);
digitalWrite(pinRangeSensor, LOW);
delayMicroseconds(2);
digitalWrite(pinRangeSensor, HIGH);
delayMicroseconds(5);
digitalWrite(pinRangeSensor, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose rangeSensorDuration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pinRangeSensor, INPUT);
rangeSensorDuration = pulseIn(pinRangeSensor, HIGH);
// convert the time into a distance
rangeSensorInches = microsecondsToInches(rangeSensorDuration);
rangeSensorCentimeters = microsecondsToCentimeters(rangeSensorDuration);
// append to serial output
Serial.print(", [(range) duration:");
Serial.print(rangeSensorDuration);
Serial.print(", ");
Serial.print(rangeSensorInches);
Serial.print("in, ");
Serial.print(rangeSensorCentimeters);
Serial.print("cm");
}
void onThermistorEvent(int value)
{
Serial.print("], thermistor: ");
Serial.print(value);
}
void onLightSensorEvent(int value)
{
// append to serial output
Serial.print(", light: ");
Serial.print(value);
// if "dark"
if (value < 700)
{
// turn the LED on
activateLED(pinLED);
// open pinRelay
openRelay();
}
else
{
// turn the LED off
deactivateLED(pinLED);
// close pinRelay
closeRelay();
}
}
void activateLED(int pinLED)
{
digitalWrite(pinLED, LOW);
}
void deactivateLED(int pinLED)
{
digitalWrite(pinLED, HIGH);
}
void openRelay()
{
digitalWrite(pinRelayLED, HIGH);
digitalWrite(pinRelay, LOW);
// turn the relay LED on
activateLED(pinRelayLED);
}
void closeRelay()
{
digitalWrite(pinRelayLED, LOW);
digitalWrite(pinRelay, HIGH);
deactivateLED(pinRelayLED);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 73.746 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment