Arduino Asked by Noman Gulzar on January 10, 2021
I wrote a code to move linear actuator and lights up and down using timer. But its not getting Sensor input please help me in this.
#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
#define interruptPin 2 //Pin we are going to use to wake up the Arduino
int uv1 = 7; //light
int uv2 = 8; // light
int mot1 = 5; //motion down
int mot2 = 6; // motion up
int buzz=39;
const int speaker = 27;
int led = 9;
int ledState = LOW;
int inputPin = 10; // choose the input pin (for PIR sensor)
int pirState = 0; // variable for reading the pin status, we start, assuming no motion detected
long previousMillis = 0;
long interval = 2000;
long OnTime = 30000;
long SenseDel = 8000; // milliseconds of on-time
long OffTime = 8000;
unsigned long timestart = 0;
const int ply = 23; // relay to reset the sound
const int stp = 35; //relay to puase sound
bool sound = false;
long time_taken = 0;
int start_delay = 3000;
int audio_time = 8000;
int audio_time1 = 8000;
int audio_time3 = 8000;
void setup()
{
pinMode(led, OUTPUT);
pinMode(uv1, OUTPUT);
pinMode(uv2, OUTPUT);
pinMode(mot1, OUTPUT);
pinMode(mot2, OUTPUT);
pinMode(led, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
digitalWrite(mot1, HIGH);
digitalWrite(mot2, LOW);
digitalWrite(led, LOW);
pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
Serial.begin(9600);
pinMode(ply, OUTPUT);
pinMode(stp, OUTPUT);
pinMode(speaker, OUTPUT);
// TIMER 1 for interrupt frequency 300.0018750117188 Hz:
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
// set compare match register for 300.0018750117188 Hz increments
OCR1A = 53332; // = 16000000 / (1 * 300.0018750117188) – 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 1 prescaler
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // allow interrupts
digitalWrite(speaker,LOW);
audio_message();
delay(200);
}
ISR(TIMER1_COMPA_vect)
{ //interrupt commands for TIMER 1 here
pirState = digitalRead(inputPin);
}
void loop()
{
unsigned long currentMillis = millis();
while ((pirState == HIGH) && (currentMillis-previousMillis >=SenseDel))
{
//previousMillis = currentMillis;
Serial.println("Motion detected!");
digitalWrite(mot2, LOW);
digitalWrite(mot1, LOW);
digitalWrite(uv1, HIGH);
digitalWrite(uv2, HIGH);
//delay(SenseDel);
//Go_To_Sleep();
}
// We only want to print on the output change, not state
//pirState = HIGH;
while ((pirState == LOW) && (currentMillis-previousMillis >= OnTime))
{
previousMillis = currentMillis;
Serial.println("Moving Down");
digitalWrite(mot2, HIGH);
digitalWrite(mot1, HIGH);
digitalWrite(uv1, LOW);
digitalWrite(uv2, LOW);
pirState = HIGH;
}
while ((pirState == LOW) && (currentMillis-previousMillis >= OffTime))
{
//previousMillis = currentMillis;
Serial.println("Stay There!");
digitalWrite(mot2, HIGH);
digitalWrite(mot1, LOW);
digitalWrite(uv1, LOW);
digitalWrite(uv2, LOW);
pirState = HIGH;
}
while ((pirState == LOW) && (currentMillis-previousMillis >= OnTime))
{
//previousMillis = currentMillis;
Serial.println("Moving Up!");
digitalWrite(mot2, LOW);
digitalWrite(mot1, LOW);
digitalWrite(uv1, LOW);
digitalWrite(uv2, LOW);
tone(buzz,2000);
delay(1000);
noTone(buzz);
delay(1000);
pirState = HIGH;
}
}
void Go_To_Sleep()
{
sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
sleep_cpu();//activating sleep mode
Serial.println("just woke up!");//next line of code executed after the interrupt
audio_message();
}
void wakeUp()
{
Serial.println("Interrrupt Fired");//Print message to serial monitor
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt from pin 2;
}
void stop_button() // func to act as stop sound
{
digitalWrite(stp, LOW);
delay(200); //100
digitalWrite(stp, HIGH);
}
void play_button() // // func to act as play sound
{
digitalWrite(ply, LOW);
delay(140);//100
digitalWrite(ply, HIGH);
}
void audio_message()
{
digitalWrite(speaker, HIGH);
play_button();
sound = true;
delay(100);
//digitalWrite(speaker, LOW);
Serial.println("Speaker On");
play_button();
//time_taken = millis();
delay(audio_time1);
delay(audio_time);
//delay(audio_time3);
//Do something…..
play_button();
stop_button();
sound = false;
digitalWrite(speaker, HIGH);
Serial.println("Speaker Closed");
delay(1000);
}
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP