Skip to content

Instantly share code, notes, and snippets.

@jci
Created March 18, 2018 03:29
Show Gist options
  • Save jci/50568bc150e6191610fd5aa37aeecffe to your computer and use it in GitHub Desktop.
Save jci/50568bc150e6191610fd5aa37aeecffe to your computer and use it in GitHub Desktop.
Guess what this does
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const int ledPin = 13; //pin 13 built-in led
const int soundPin = A0; //sound sensor attach to A0
int temporal = 0;
const int sweetspotdown = 150; // change this to the sweetspot value
int value = 0;
const int silenceup = 350;
const int silencedown = 290;
const int silence = 320;
const int alarmthreshold = 100;
const int alarmtrigger = 400;
const int volumeup = 15;
const int volumedown = 10;
long int contador = 0;
int thresholdup = 600;
int pinvalue =0;
const int mydelay = 100; // 50 ms
Servo myservo;
LiquidCrystal_I2C lcd(0x3f, 20, 4);
/*
*
*
150 mid value en 3V
*
*/
void setup()
{
pinMode(ledPin, OUTPUT);
myservo.write(0);
Serial.begin(115200); //initialize serial
myservo.attach(4,700,2300); // pin 4
// lcd crap
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("ALARM CLOCK");
lcd.setCursor(0, 1);
lcd.print("(C) jci");
myservo.write(150);
digitalWrite(ledPin, LOW);
myservo.write(0);
delay(1000);
digitalWrite(ledPin, HIGH);
myservo.write(150);
delay(1000);
myservo.write(0);
}
void loop()
{
while (true)
{
lcd.setCursor(0, 0);
lcd.print("NOISE LEVEL ");
pinvalue = analogRead(soundPin);
value = abs(pinvalue - silence); // arduino says fuck math!
if (value > alarmthreshold)
{
// things that happen when value above threshold
contador += volumeup;
}
else
{
// things that happen when value below threshold
contador -= volumedown;
}
if (contador <0 ) contador =0;
if (contador > alarmtrigger)
{
lcd.setCursor(0, 0);
lcd.print("BITCH BETTUH ");
lcd.setCursor(0, 1);
lcd.print("HAVE MY MONEY ");
for (int temp=0; temp<10; temp++)
{
myservo.write(0);
delay(2000);
myservo.write(150);
delay(2000);
}
myservo.write(0);
contador = 0;
}
/*
Serial.print(value);
Serial.print(",");
Serial.println(abs(contador));
// Serial.print(",");
// Serial.println(pinvalue);
*/
if (contador == 0)
{
lcd.setCursor(0, 1);
lcd.print(" ");
}
if ((contador > 0) && (contador<alarmtrigger/4))
{
lcd.setCursor(0, 1);
lcd.print("XXXX ");
}
if ((contador> alarmtrigger/4) && (contador<alarmtrigger/2))
{
lcd.setCursor(0, 1);
lcd.print("XXXXXXXXX ");
}
if ((contador> alarmtrigger/2) && (contador<alarmtrigger*0.75))
{
lcd.setCursor(0, 1);
lcd.print("XXXXXXXXXXXXX ");
}
delay(mydelay); // end of cycle
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment