Skip to content

Instantly share code, notes, and snippets.

@krobro
Created February 28, 2015 01:34
Show Gist options
  • Save krobro/8a8ab341060309f4f2bf to your computer and use it in GitHub Desktop.
Save krobro/8a8ab341060309f4f2bf to your computer and use it in GitHub Desktop.
//
// ISeeLight
//
// Print the value of the light sensor to the Serial Monitor.
// Also, make electronic music ... like a theremin.
//
// We are using port S2 on the Duinobot board for the light sensor.
//
// NOTES:
// - Ports S0-S5 are analog input pins
// - S0 is identical to Arduino A0 and so on (S5=A5)
// - To use these ports as Digital pins add 14
//
#define S2 2 // do not add 14 becuase this is analog not digital
int lightLevel;
// run once, when the sketch starts
void setup() {
// Setup light sensor ...
// We actually do not have to setup the light sensor port because
// the default behaviour of these ports is to receive analog input
// setup the speaker ... this is our theremin
pinMode(SPEAKER, OUTPUT);
// Setup serial communication ...
Serial.begin(9600);
}
// run over and over again
void loop() {
int pitch;
// get the light sensor state
lightLevel = analogRead(S2);
// take the raw reading from the light sensor,
// (a number between 0 and 1023) and turn it into
// an audible frequency (or pitch) we can hear
//pitch = 200 + lightLevel/4;
pitch = 20 + lightLevel/2;
tone(SPEAKER, pitch);
// Print the state of the sensor to the Serial Monitor
Serial.print("The light level reads: ");
Serial.print(lightLevel);
Serial.print(" (pitch is: ");
Serial.print(pitch);
Serial.println(")");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment