Hi,
I'm try to make an AlarmSignal that turns on a LED (it does not say what kind, it is a powerled 12v AC/DC in a white cover) every 5 minutes for half a minute. When the cup (to which the accelerometer is attached) is picked up within this half a minute, the light should fade off. When the cup is shaked harder than just picked up, it should immediately go the AlarmSignal. The LED used to turn on under these circumstances, but apperanlty I changed something in the code that stops the LED from turning on. In the serial monitor I see that it activates the reset, detects the movement but the serial print of the LED gives a 6 all the time and the light stays off. Anyone any idea?
This is the code:
#include <Time.h>
#include <TimeAlarms.h>
#include <TinkerKit.h>
int acc = I0;
int led = 6;
int NTC = 5;
long interval = 5000;
boolean detected_movement = false;
boolean detected_heat = false;
boolean detected_reset = false;
void setup()
{
Serial.begin(9600);
Alarm.timerRepeat(300, AlarmSignal); // timer for every .. seconds
pinMode (acc, INPUT);
pinMode(NTC, INPUT);
pinMode(led, OUTPUT);
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
DetectMovement();
DetectHeat();
DetectReset();
}
void DetectReset()
{
//A hard shake means the program is reset!
detected_reset = analogRead(acc) >= 500 ; //random(26) >= 10;//
if(detected_reset){
Serial.println("Reset DETECTED");
delay(3000);
Serial.println("start reset");
AlarmSignal();
}
}
void DetectMovement()
{
boolean previous_detected_movement = detected_movement;
detected_movement = analogRead(acc)>= 460 && analogRead(acc) <= 470 || analogRead(acc) >= 490 && analogRead(acc) <=499 ;
Serial.println(analogRead(acc));
// Serial.println(analogRead(NTC));
if(detected_movement)
{
Serial.println("Movement1 DETECTED");
}
if(previous_detected_movement == false && detected_movement == true)
{
FadeOut(100);
}
}
//Fade in if heat is detected
// if(previous_detected_heat == false && detected_heat == true)
// detected_movement = analogRead(acc) >= 500 && analogRead(acc) < 510; //random(26) >= 10;//
// Serial.println(analogRead(acc));
// if(detected_movement==true)
// {
// Serial.println("Movement1 DETECTED");
// FadeOut(100);
// }
//}
void DetectHeat()
{
boolean previous_detected_heat = detected_heat;
detected_heat = analogRead(NTC) >= 500;
// Serial.println(analogRead(NTC));
if(detected_heat)
{
Serial.println("Heat DETECTED");
}
//Fade in if heat is detected
if(previous_detected_heat == false && detected_heat == true)
{
delay(3000);
FadeIn(30);
delay(1000);
FadeOut(30);
}
}
void FadeIn(int fade_delay)
{
//Fade in
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { // sets the value (range from 0 to 255):
analogWrite(led, fadeValue);
delay (fade_delay);
digitalWrite(led, HIGH);
}
}
void FadeOut(int fade_delay)
{
//fade out
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { // sets the value (range from 0 to 255):
analogWrite(led, fadeValue); // wait for 30 milliseconds to see the dimming effect
delay(30);
analogWrite(led, LOW);
}
}
void AlarmSignal(){
unsigned long previousMillis = millis();
if(!detected_movement and !detected_heat)
{
FadeIn(30);
unsigned long currentMillis = millis();
//Wait for interval
while(currentMillis - previousMillis < interval and !detected_movement and !detected_heat and !detected_reset)
{
currentMillis = millis();
DetectReset();
DetectMovement();
DetectHeat();
}
if (!detected_movement and !detected_reset and !detected_heat)
{
FadeOut(30);
}
}
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}