Pages: [1]   Go Down
Author Topic: LED PWM control with PIR and RTC  (Read 1038 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Having problem with code, could not figure it out right now.
PIR triggers but no output.
Code:
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>

int pir = 12;
const int led4 = 11;
const int led3 = 10;
const int led2 = 9;
const int led1 = 8;

int led1val;
int led2val;
int led3val;
int led4val;

RTC_DS1307 RTC;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(pir, INPUT);
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  DateTime now = RTC.now();
  setTime(hour(), minute(), second(), day(), month(), year());
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year());

  // Alarmid (Hour, Minute, Second)
  Alarm.alarmRepeat(20,0,0, Alarm1);  //
  Alarm.alarmRepeat(17,15,0, Alarm2);  //
  Alarm.alarmRepeat(8,0,0, Alarm3); //
  Alarm.alarmRepeat(21,30,0, Alarm4);  //
  Alarm.alarmRepeat(10,0,0, Alarm5);  //
  Alarm.alarmRepeat(15,0,0, Alarm6);  //
  //  Alarm.alarmRepeat(0,0,0, Alarm7);  //
  //  Alarm.alarmRepeat(0,0,0, Alarm8);  //
  //  Alarm.alarmRepeat(0,0,0, Alarm9);  //
  //  Alarm.alarmRepeat(0,0,0, Alarm10);  //

}

void  loop(){
  DateTime now = RTC.now();
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); // set time & date
  Alarm.delay(1000); //clock display delay

  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  motion();
}

void motion(){
  if (pir = HIGH);
  {
    digitalWrite(led1, led1val);
    digitalWrite(led2, led2val);
    digitalWrite(led3, led3val);
    digitalWrite(led4, led4val);
  }
}
// Alarm Functions
void Alarm1(){
  int led1val = 30;
  int led2val = 0;
  int led3val = 50;
  int led4val = 0;
}

void Alarm2(){
  int led1val = 255;
  int led2val = 100;
  int led3val = 30; 
  int led4val = 0;

}
void Alarm3(){
  int led1val = 255;
  int led2val = 255;
  int led3val = 255; 
  int led4val = 0;
}

void Alarm4(){
  int led1val = 100;
  int led2val = 0;
  int led3val = 100; 
  int led4val = 100;
}

void Alarm5(){
  int led1val = 0;
  int led2val = 0;
  int led3val = 0; 
  int led4val = 0;
}
void Alarm6(){
  int led1val = 255;
  int led2val = 0;
  int led3val = 0;
  int led4val = 30; 
}


Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Get rid of int from all your alarm functions - you're declaring local variables which mask the globals you're trying to set.

Three classic errors here:
Code:
  if (pir = HIGH);

pir is the pin number, not the status of the pin. Use digitalRead.

= is assignment, not a test for equality. Use ==

There should be no semicolon there.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like its working right now. Thank you.
Code:
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>

int pir = 12;
const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;

int led1val = 255;
int led2val = 100;
int led3val = 0;
int led4val = 255;
RTC_DS1307 RTC;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(pir, INPUT);
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  DateTime now = RTC.now();
  setTime(hour(), minute(), second(), day(), month(), year());
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year());

  // Alarmid (Hour, Minute, Second)
  Alarm.alarmRepeat(8,45,0, Alarm1);  //
  Alarm.alarmRepeat(16,0,0, Alarm2);  //
  Alarm.alarmRepeat(17,30,0, Alarm3); //
  Alarm.alarmRepeat(19,30,0, Alarm4);  //
  Alarm.alarmRepeat(20,30,0, Alarm5);  //
  Alarm.alarmRepeat(21,30,0, Alarm6);  //
  Alarm.alarmRepeat(23,0,0, Alarm7);  //
  Alarm.alarmRepeat(6,45,0, Alarm8);  //
  //  Alarm.alarmRepeat(0,0,0, Alarm9);  //
  //  Alarm.alarmRepeat(0,0,0, Alarm10);  //

}

void  loop(){
  DateTime now = RTC.now();
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); // set time & date
  Alarm.delay(1000); //clock display delay

  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
 
  motion();
  }



void motion(){
  if (digitalRead(pir) == HIGH)
  {
    analogWrite(led1, led1val);
    analogWrite(led2, led2val);
    analogWrite(led3, led3val);
    analogWrite(led4, led4val); delay(10000);
  }
  else {
    analogWrite(led1, 0);
    analogWrite(led2, 0);
    analogWrite(led3, 0);
    analogWrite(led4, 0);
  }
}
//  void debug() {
//  delay(1000);
//  Serial.print("LED1"); 
//    Serial.print('~'); 
//    Serial.println(led1val);
//    Serial.print("LED2"); 
//    Serial.print('~'); 
//    Serial.println(led2val);
//    Serial.print("LED3"); 
//    Serial.print('~'); 
//    Serial.println(led3val);
//    Serial.print("LED4"); 
//    Serial.print('~'); 
//    Serial.println(led4val);
//  }
// Alarm Functions
void Alarm1(){
  led1val = 150;
  led2val = 100;
  led3val = 0;
  led4val = 0;
}

void Alarm2(){
  led1val = 150;
  led2val = 255;
  led3val = 0; 
  led4val = 50;

}
void Alarm3(){
  led1val = 180;
  led2val = 150;
  led3val = 0; 
  led4val = 50;
}

void Alarm4(){
  led1val = 100;
  led2val = 150;
  led3val = 50; 
  led4val = 11;
}

void Alarm5(){
  led1val = 150;
  led2val = 60;
  led3val = 30; 
  led4val = 100;
}
void Alarm6(){
  led1val = 150;
  led2val = 80;
  led3val = 30;
  led4val = 50; 
}
void Alarm7(){
  led1val = 130;
  led2val = 60;
  led3val = 30;
  led4val = 0; 
}
void Alarm8(){
  led1val = 100;
  led2val = 100;
  led3val = 30;
  led4val = 0; 
}









« Last Edit: September 11, 2014, 08:33:32 am by ahoi » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any ideas how i could add different delay to each LED output? Like how long each LED is on.
Turning LEDs on should be instant when pir goes high but turning off each LED in different time "delay(xxx)" dosent help me in this i guess..
« Last Edit: September 11, 2014, 08:38:27 am by ahoi » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With a RTC, did you mean a Real Time Clock ?? It would be of great help that you will show us how did you connected your electronic, maybe by using FRITZING or any other software.. I have used the sensor PIR and the RGB led in other experiments so maybe I could help you in something..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be of great help that you will show us how did you connected your electronic,
Check this: https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/wiring-it-up
Logged

Pages: [1]   Go Up
Jump to: