Hi,
I'm currently experimenting with a Digispark.
having come in from the Raspberry pi side - I'm accustomed to being able to push tasks to the background and starting another.
but given the single-task nature of the digispark, I have to be creative.
What I'm currently trying to do is to count the times that Void loop completes, and when it hits a certain number, perform an action.
The way I'm doing this is a trick I have used in VB.net and VB6 for years.
//begin
Variable = 0
//on each cycle
Variable + Variable + 1
if variable = 80000000 then perform task
variable = 0 again.
The problem I'm running into is getting it to reset.
I have posted my code - and I know it's a bit messy, but I have tried to add code notations to make life easier.
I have avoided pin 5 as I'm using a chinese digispark with pin 5 set to reset in factory firmware.
my code runs fine until the "else" loop.
/*
Doorbell beam code
Attached to long range IR perimiter beam relay.
Designed to trigger wireless doorbell immediately on pin 2
Then trigger external lights and sound with delay,
to disguise the physical position and reaction time of the beam.
*/
const int buttonPin = 0; // the number of the pushbutton pin
const int ledPin = 1; // the number of the LED pin
double CycleCounter = 0;
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(ledPin, OUTPUT); //connected to wireless doorbell transmitter
pinMode(buttonPin, INPUT); //connected to IR beam sensor output
pinMode(2, OUTPUT); //connected to "Range active" sign
pinMode(3, OUTPUT); //connected to warning beeper
pinMode(4, OUTPUT); //connected to external building light (intended to trigger from cycle count)
//pinMode(5, OUTPUT);
digitalWrite(2, HIGH); //turn all relays off (High = off, Low = on)
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(1, HIGH);
}
void loop() {
// read the state of the IR beam relay
buttonState = digitalRead(buttonPin);
// check if the IR beam is broken. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
//Trip the doorbell button for 250 miliseconds
digitalWrite(2, LOW);
delay(250);
digitalWrite(2, HIGH);
//wait two seconds
delay(2000);
//turn on a light relay
digitalWrite(4, LOW);
//flash "Range active" sign
digitalWrite(3, LOW);
delay(250);
digitalWrite(3, HIGH);
delay(250);
digitalWrite(3, LOW);
delay(250);
digitalWrite(3, HIGH);
delay(250);
digitalWrite(3, LOW);
delay(250);
digitalWrite(3, HIGH);
delay(250);
digitalWrite(3, LOW);
delay(250);
digitalWrite(3, HIGH);
delay(250);
//end flashing sign.
//turn off light relay
digitalWrite(4, HIGH);
} else {
// make sure doorbell transmitter is not continuously triggered
digitalWrite(ledPin, HIGH);
//Check cycle count
if (CycleCounter = 80000000) {
digitalWrite(2, LOW);
delay(2000);
CycleCounter = 0;
digitalWrite(2, LOW);
}
//add to the cycle counter
CycleCounter = CycleCounter + 1;
}
}