Skip to content

Instantly share code, notes, and snippets.

@ybakos
Last active November 30, 2020 15:34
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 ybakos/8ffc54607115da6e353c7da86a97d9f2 to your computer and use it in GitHub Desktop.
Save ybakos/8ffc54607115da6e353c7da86a97d9f2 to your computer and use it in GitHub Desktop.
CS 160 SparkFun Sandbox Project
// Project: Code controls circuitry.
// Outputs: lights
// Inputs: sensors (slider, microphone, light sensor)
//
// This program has three components:
// - light detection
// - temperature detection
// - sound detection
const int temperatureSensor = A0;
const int lightSensor = A1;
const int microphone = A2;
const int slider = A3;
const int D4 = 4;
const int D5 = 5;
const int D6 = 6;
const int D7 = 7;
const int D8 = 8;
const int D9red = 9;
const int D10green = 10;
const int D11blue = 11;
const int D13 = 13;
void setup() {
// Configure inputs
pinMode(temperatureSensor, INPUT);
pinMode(lightSensor, INPUT);
pinMode(microphone, INPUT);
pinMode(slider, INPUT);
// Configure outputs (lights)
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(D9red, OUTPUT);
pinMode(D10green, OUTPUT);
pinMode(D11blue, OUTPUT);
}
void loop() {
// Light detection
const int DARK = 10;
int lightValue = analogRead(lightSensor);
if (lightValue <= DARK) {
digitalWrite(D13, HIGH);
} else {
digitalWrite(D13, LOW);
}
// Temerature detection
const float WARM = 70.0;
long rawTemperatureValue = analogRead(temperatureSensor);
float voltage = rawTemperatureValue * (5 / 1023.0);
float celsius = (voltage - 0.5) * 100;
float fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
if (fahrenheit >= WARM) {
analogWrite(D9red, 255);
analogWrite(D10green, 100);
analogWrite(D11blue, 100);
} else {
analogWrite(D9red, 0);
analogWrite(D10green, 0);
analogWrite(D11blue, 255);
}
// Sound detection
int maximumSoundLevel = analogRead(slider);
int levelOfSound = analogRead(microphone);
const int lights[5] = {D4, D5, D6, D7, D8};
for (int i = 0; i < 5; ++i) {
if (levelOfSound > ((maximumSoundLevel / 5) * (i+1))) {
digitalWrite(lights[i], HIGH);
} else {
digitalWrite(lights[i], LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment