Pages: [1]   Go Down
Author Topic: Alarm Clock Hack, interfacing with Servo, Input Signals, and Motion Sensor  (Read 2351 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all, I'm pretty new to this still after playing with it for a little. I figured now is the time to use this in a real world application that I would actually use. I'll now explain the scope of the project. As of now I have an alarm that reads a signal from an already existing alarm clock from analog. Once the alarm clock goes off it goes into a light sequence using a dimmer switch and a servo to control the dimmer. After that has completed a buzzer goes off and then I have a driveway alert sensor from harbor freight where the alarm wont go off until there is motion in some other room. Theres really only 2 inputs which are variable. Now I have already wrote a rough program that somewhat works. It seems if I let it run overnight it messes up in the morning. I have a feeling this is something to do with memory. Just don't know where this problem lies. Let me know if you guys have any incite. Bare with my code as I'm pretty bad at writing code but this is what I came up with. Thanks.

Code:
//Alarm Clock with Servo Dimmer, Buzzer and LED Indicator!!!
//Rewrite Via VBB

#include <Servo.h>

Servo myservo;
const int proxPin = 1;                   //proximity sensor
const int analogPin = 0;                //alarm input
const int ledPin = 13;                   //led
const int buzzerPin = 12;             //buzzer
const int threshold = 940;             //threshold for proximity
const int alarmthres = 550;           //threshold for alarm
const int ledPin1 = 3;                   //led1

int pos = 0;                                //default position for servo
int alarmInputCounter = 0;           //counter setup for alarm
int alarmState = 0;                     //state of alarm (on or off)
int lastAlarmState = 0;                //last state of alarm
int proxInputCounter = 0;            //counter setup for prox
int proxState = 0;                      //state of prox (on or off)
int lastProxState = 0;                 //last state of prox
int resetSwitch = 0;                   //state of reset (reset now)
int buzzerState = 0;                   //state of buzzer (on or off)
int buzzerCurrent = LOW;           //buzzer current to use with millis
int proxSwitch = 0;                    //prox switch to indicate when to start looking for prox state
long previousMillis = 0;
long interval = 1000;

void setup() {
  myservo.attach(9);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(analogPin);
  int proxValue = analogRead(proxPin);
  long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval){
    previousMillis = currentMillis;
    if(buzzerCurrent == LOW){
      buzzerCurrent = HIGH;
    }else{
      buzzerCurrent = LOW;
    }
  }
 
  if(analogValue > alarmthres) {
    alarmState = 1;
  } else {
    alarmState = 0;
  }
 
  if(alarmState != lastAlarmState) {
    if(alarmState == 1) {
      alarmInputCounter++;
    }
  }
  lastAlarmState = alarmState;
 
  if(alarmInputCounter >= 1){
    pos++;
    digitalWrite(ledPin, HIGH);
  }
  if(pos <= 160) {
    myservo.write(pos);
    delay(100);
  } else {
    myservo.write(170);
    delay(20);
    buzzerState = 1;
    proxSwitch = 1;
  }
   
  if (buzzerState >= 1){
    digitalWrite(buzzerPin, buzzerCurrent);
  }
 
  if(proxSwitch >= 1) {
    if(proxValue > threshold){
      proxState = 1;
    } else {
      proxState = 0;
    }
  }

  if (proxState >= 1) {
    buzzerState = 0;
    delay(500);
    digitalWrite(ledPin1, HIGH);
    myservo.write(0);
    delay(500);
    digitalWrite(ledPin1, LOW);
    delay(200);
    digitalWrite(ledPin1, HIGH);
    delay(200);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(10000);
    software_Reset();
   
  }
 
  Serial.println(pos);
  Serial.println(buzzerState);
  Serial.println(analogValue);
}

void software_Reset()
{
  asm volatile ("  jmp 0");
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well... I just realized that I dont need the counters at all. I have a hard time with "IF" statements. All i needed was an indicator that told me when the alarm signal went high once. I think that since its just sitting there counting forever that it fills up the memory over a longer period of time. All i needed was below.

if(alarmValue > alarmThres){
   alarmState = 1;
}

Did not realize that I don't have to indicate an else command. My else command would have made it go back to 0 so thats where I thought I needed the counter. This works better but its still unknown if this is the cure. I'll keep you guys posted. I'm a newb from hell...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what I have come up with so far... it's now getting stuck on the reset sequence. I think its actually where my servo should return back to 0. It seems like it starts too then the arduino locks up. I put the pos to 0 instead of 175 in the else command... I dont know where its doing it now...
Logged

USA
Offline Offline
God Member
*****
Karma: 14
Posts: 644
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what I have come up with so far... it's now getting stuck on the reset sequence. I think its actually where my servo should return back to 0. It seems like it starts too then the arduino locks up. I put the pos to 0 instead of 175 in the else command... I dont know where its doing it now...

Probably because you are calling a function that's located in the code after the main loop that is apparently attempting to jump to the beginning of your entire code, including the include statement and initial definitions...  The loop() function is a prepetual loop, so it will eventually go back to the beginning of the loop without any reset function.  If you absolutely want to jump around your code, and while it isn't necessary for this sketch it's occasionally useful, use the goto command instead of trying to do it in assembly. 

However a better way would be to get rid of the reset function and instead have the "if (proxState >= 1)" code block as follows...
Code:
if (proxState >= 1) {
    buzzerState = 0;
    delay(500);
    digitalWrite(ledPin1, HIGH);
    myservo.write(0);
    delay(500);
    digitalWrite(ledPin1, LOW);
    delay(200);
    digitalWrite(ledPin1, HIGH);
    delay(200);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(10000);
    pos = 0;
 }
   
Logged

Pages: [1]   Go Up
Jump to: