Skip to content

Instantly share code, notes, and snippets.

@dclobato
Created September 30, 2015 18:40
Show Gist options
  • Save dclobato/83545b827ce6909f71af to your computer and use it in GitHub Desktop.
Save dclobato/83545b827ce6909f71af to your computer and use it in GitHub Desktop.
Codigo para rodar no Spark Photon e funcionar com o watchdog no ATTiny85
/*
Spark Cyan Flash of Death Killer - by Scott Lichtenstein (StartGroup)
Open Sourcec - Please feel free to do whatever you want with this! :)
*/
const int resetButton = 0; // pin that will flip the reset switch
int sensorValue = 0; // variable to store the value coming from the Spark pin
int sensorPin = 3; // pin from Spark Core
int sensorState; // holds current state of pin from Spark Core
int lastTriggerState = LOW; // the last successful read of the Spark Core pin
long lastTriggerTime = 0; // the last time the state was changed
void setup() {
delay(10000); // wait 10 seconds for the core to connect.
pinMode(resetButton, OUTPUT); // call the resetbutton pin as an output
pinMode(sensorPin, INPUT); // call the sernsorPin as an input
digitalWrite(resetButton, HIGH); // Keep reset pin at high until further notice
}
void loop() { // here starts my loop
setState(); // checks the sensor and sets value to 1 or 0 depending on its value
if (sensorState != lastTriggerState) { // checks if pin is the same or different from last time.
lastTriggerTime = millis(); // sets new time for change
lastTriggerState = sensorState; // changes new state
}
if ((millis() - lastTriggerTime)>10000){ // if it has been more than 10 seconds since the state has changed, do a reset
reset(); // activate reset function
}
}
void reset(){ // here is the reset function
digitalWrite(resetButton,LOW); // Push Reset button
delay(1000); // Wait
digitalWrite(resetButton,HIGH); // Let go of Reset button
delay(30000); // wait 30 deconds for reconnect prior to checking again
}
void setState(){ // here is the set state function, number varibles are just to identify change in state.
sensorValue = analogRead(sensorPin); // check for spark pin
if (sensorValue < 500) // if analog value is less than 500, set to variable 0
{
sensorState = 0; // variable 0
}
if (sensorValue > 500) // if analog state is more than 500, set to varible 1
{
sensorState = 1; // varible 1
}
}
int trigger = A7; // this is the pin talking to the attiny
int led = D7; // this is the led pin for a visual indicator that things are running, it is optional
uint32_t lastKick = 0; // last time we kicked the watch dog
bool s = false; // output state of the watch dog
void setup()
{
lastKick = millis(); // We just powered up
pinMode(trigger, OUTPUT); // set signal pin to output
pinMode (led, OUTPUT); // set led pin to output
}
void loop() {
kickTheDog(); // kick the watchdog
////////////////////////////
// place normal code here //
////////////////////////////
} // End main loop (currently runs every 5-6 ms)
void kickTheDog() { // kick the dog every 5 seconds
if( (millis()-lastKick) > 5000 ) {
lastKick = millis();
s = !s; // toggle the outputs
digitalWrite(led,s); // watchdog indicator
digitalWrite(trigger,s); // watchdog signal
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment