Last active
July 2, 2018 17:32
-
-
Save Iainmon/eb7ba9eb895c8635dcee5d8bd14745bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int knobPin = 3; | |
const int onButtonPin = 2; | |
const int relayPin = 4; | |
const int potPin = 2; //analog | |
int onButtonState = 0; | |
int potVal; | |
bool onState; | |
int hertz; | |
int uptime; | |
int downtime; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(onButtonPin, INPUT); | |
pinMode(knobPin, INPUT); | |
pinMode(relayPin, OUTPUT); | |
onState = false; | |
} | |
void loop() { | |
onButtonState = digitalRead(onButtonPin); | |
potVal = analogRead(potPin); | |
if (onState) { | |
programLoop(); //program | |
} | |
//changes state of program. | |
if (onButtonState == HIGH) { | |
if (onState) { | |
onState = false; | |
//deinitialize all | |
toggleRelay(false); | |
} else { | |
onState = true; | |
} | |
delay(1000); | |
} | |
} | |
void programLoop() { | |
uptime = map(potVal, 0, 1023, 750, 2250); //(potentiometer input maps to:, min analog, max analog, 30% of 25000, 90% of 2500); | |
downtime = 25000 - uptime; | |
occilate(); | |
} | |
void toggleRelay(bool state) { | |
if (state) { | |
digitalWrite(relayPin, HIGH); | |
} else { | |
digitalWrite(relayPin, LOW); | |
} | |
} | |
void occilate() { | |
toggleRelay(true); | |
microsDelay(uptime); | |
toggleRelay(false); | |
microsDelay(downtime); | |
} | |
void microsDelay(int micros1) { | |
while (micros() >= (micros() + micros1)) {} | |
} | |
int changeBrightness(float percent) { | |
int Brightness =((int)(percent * 100)); | |
Brightness = map(Brightness, 0, 1023, 0, 100); | |
analogWrite(transPin, Brightness); | |
return Brightness; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment