Skip to content

Instantly share code, notes, and snippets.

@JeremySCook
Created May 6, 2021 14:15
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 JeremySCook/0c4f5e19ca86a40cff44b0595a880784 to your computer and use it in GitHub Desktop.
Save JeremySCook/0c4f5e19ca86a40cff44b0595a880784 to your computer and use it in GitHub Desktop.
light controller
//A7 low A2 low for button input
//A0 input 1, A5 input 2
//A7 is not going low for some reason when defined as a low output - did not work with knockoff
//board, but works with Nano Every
#define button1 A0
#define button2 A5
#define PIRInput 12
int analogValue = 20;
int analogMap = 51;
bool button1State = 1;
bool button2State = 1;
bool PIRState = 0;
long previousMillis = 0;
unsigned long currentMillis = 0;
long stayOn = 600000; //how long to stay on after triggered 600000 = 10 minutes
void setup() {
// set A2, A7 to LOW permanently, D11 HIGH
pinMode(A2, OUTPUT);
digitalWrite(A2, LOW);
pinMode(A7, OUTPUT);
digitalWrite(A7, LOW);
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
// set A0, A5 to input pullups
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(PIRInput, INPUT); //PIR SENSOR
pinMode(9, OUTPUT); //MOSFET output
pinMode(13, OUTPUT); //indiactor LED
Serial.begin(9600);
}
void loop() {
button1State = digitalRead(button1);
button2State = digitalRead(button2);
PIRState = digitalRead(PIRInput);
currentMillis = millis();
if(button2State == LOW && analogValue <= 80){
analogValue += 20;
analogMap = map(analogValue, 0, 100, 0, 255);
}
if(button1State == LOW && analogValue >= 20){
analogValue -=20;
analogMap = map(analogValue, 0, 100, 0, 255);
}
if(PIRState == HIGH){
previousMillis = currentMillis;
}
if((currentMillis - previousMillis) < stayOn){
analogWrite(9, analogMap);
analogWrite(13, analogMap);
}
if((currentMillis - previousMillis) >= stayOn){
analogWrite(9, 25);
//analogWrite(13, 51);v //onboard LED, can be used for testing
}
/*
Serial.print(button1State); Serial.print(" "); Serial.print(button2State);
Serial.print(" "); Serial.print(analogValue); Serial.print(" "); Serial.print(analogMap);
Serial.print(" PIRState "); Serial.println(PIRState);
*/
delay(100); //debounce etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment