I don't know how to program but I've managed to cobble something together from code copied from other people's work. I was wondering whether my program was compatible with the idle function and if so, do I put it between the setup and the loop? I read that it may stop the first analogue read from working after waking from idle so my solution is to just code the read to happen twice in a row. Does that make sense?
I figured I could make it sleep in the time between alarm deactivation (by push-button) and alarm activation (at 2100 hrs each day). Can it also sleep during the delays or does that make no sense?
Incidentally, I'm running at 2 Mhz with the consequence that USB won't work; does that mean that disabling the USB to save power is unnecessary?
Teensy 3.2
I figured I could make it sleep in the time between alarm deactivation (by push-button) and alarm activation (at 2100 hrs each day). Can it also sleep during the delays or does that make no sense?
Incidentally, I'm running at 2 Mhz with the consequence that USB won't work; does that mean that disabling the USB to save power is unnecessary?
Teensy 3.2
Code:
//This sketch triggers daily alarms at 2100 hrs.
#include <Time.h>
#include <TimeAlarms.h>
int buttonPin = 1; // assign pin to alarm silence button
int ledPin = 12; // assign pin to alarm signal output
int sensorPin = A2; // assign input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
int buttonState = 0; // variable to store the value of the push button
volatile int alarmState = 0; // variable to store the status of the alarm
// the setup function runs once when you press reset or power the board
void setup()
{
setTime(20,59,59,0,0,0); // set time to 20:59:59
// create the alarms
Alarm.alarmRepeat(21,0,0, LithiumAlarm); // set alarm for 2100 hrs every day
// initialize ledPin as an output and buttonPin as an input
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(sensorPin, INPUT);
Serial.begin(9600); // open the serial port at 9600 bps:
}
// functions to be called when an alarm triggers:
void LithiumAlarm(){
alarmState = 1;
}
// the loop function runs over and over again forever
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
Alarm.delay(1000);
if (alarmState==1 && sensorValue<200) {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
Alarm.delay(50); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
Alarm.delay(1000); // wait for a second
}
if (digitalRead(buttonPin) == 0) {
alarmState = 0;
}
}