Skip to content

Instantly share code, notes, and snippets.

@neon-ninja
Last active November 1, 2025 09:40
Show Gist options
  • Select an option

  • Save neon-ninja/f0ec8ce5db4c2b16bf005ff420258828 to your computer and use it in GitHub Desktop.

Select an option

Save neon-ninja/f0ec8ce5db4c2b16bf005ff420258828 to your computer and use it in GitHub Desktop.
// variable to hold sensor value
int sensorValue;
int sensorValueA1;
// variable to calibrate low value
int sensorLow = 1023;
// variable to calibrate high value
int sensorHigh = 0;
// LED pin
const int ledPin = 13;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
unsigned long lastLCDwrite;
// make some custom characters:
byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
byte smiley[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b10001,
0b01110,
0b00000
};
byte frownie[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b00000,
0b01110,
0b10001
};
byte armsDown[8] = {
0b00100,
0b01010,
0b00100,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte armsUp[8] = {
0b00100,
0b01010,
0b00100,
0b10101,
0b01110,
0b00100,
0b00100,
0b01010
};
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// create a new character
lcd.createChar(0, heart);
// create a new character
lcd.createChar(1, smiley);
// create a new character
lcd.createChar(2, frownie);
// create a new character
lcd.createChar(3, armsDown);
// create a new character
lcd.createChar(4, armsUp);
// Print a message to the LCD.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibrating...");
// Make the LED pin an output and turn it on
pinMode(ledPin, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.begin(9600);
// calibrate for the first five seconds after program runs
while (millis() < 5000) {
// record the maximum sensor value
sensorValue = analogRead(A0);
lcd.setCursor(0, 1);
String output = String(sensorLow) + "<" + String(sensorValue) + "<" + String(sensorHigh);
lcd.print(output);
if (sensorValue > sensorHigh) {
sensorHigh = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorLow) {
sensorLow = sensorValue;
}
}
// turn the LED off, signaling the end of the calibration period
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Operating...");
digitalWrite(ledPin, LOW);
}
void loop() {
//read the input from A0 and store it in a variable
sensorValue = analogRead(A0);
sensorValueA1 = analogRead(A1) / 2;
sensorValueA1 = constrain(sensorValueA1, 20, 1000);
Serial.println(sensorValueA1);
if (millis() - lastLCDwrite > 500) {
lcd.setCursor(0, 1);
String output = String(sensorLow) + "<" + String(sensorValue) + "<" + String(sensorHigh);
lcd.print(output);
lastLCDwrite = millis();
}
// map the sensor values to a wide range of pitches
int pitch = map(sensorValue, sensorLow, sensorHigh, 4000, 50);
if (sensorValue < (sensorHigh - 50)) {
tone(8, pitch, sensorValueA1 * 2);
}
int brightness = map(sensorValue, sensorLow, sensorHigh, 255, 0);
brightness = constrain(brightness, 0, 255);
analogWrite(9, brightness);
// wait for a moment
delay(sensorValueA1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment