Pages: [1]   Go Down
Author Topic: Bytes to decimals to an integer?  (Read 252 times)
0 Members and 1 Guest are viewing this topic.
Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I started this from a sketch where a PIR detector turns on a light and after 30 seconds the light goes out. This works. Earlier this week I bought a little DS1307 module and hooked it up through I2C to my Arduino Uno. Now I want to only turn on the light in the evening (lets say after 18:00) and if the PIR detector is high. For the DS1307 module I used some code I found here -> http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock
I can read the time from the DS1307 (serial monitor).
The only bit that doesn't work is this: "if (pirState == HIGH && (hour, DEC) >= 18)"  Do I need to convert the "hour"?
Could someone give me some pointers on how I can accomplish this?
This is the complete sketch:
Code:
/*
  http://www.bajdi.com
  PIR detector turns on light, light switches automatically off after 30 seconds.
  Light must only turn on in the evening (after 18:00) 
 */

#include <Wire.h>
const int DS1307_I2C_ADDRESS = 0x68;
const int pir = 2;     // the number of the PIR pin
const int light =  13;      // the number of the light pin

unsigned long on;      //start time for pir light on

int pirState = 0;         // variable for reading the pushbutton status

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}



void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0x00);
  Wire.endTransmission();
 
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 
  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.receive() & 0x7f);
  minute     = bcdToDec(Wire.receive());
  hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(Wire.receive());
  dayOfMonth = bcdToDec(Wire.receive());
  month      = bcdToDec(Wire.receive());
  year       = bcdToDec(Wire.receive());
 
}

void setup() {
  Wire.begin();
  Serial.begin(9600);
  pinMode(light, OUTPUT);     
  pinMode(pir, INPUT);
}

void loop(){

  getDateDs1307();
  Serial.println("  ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.println("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.println(year, DEC);
  delay(2000);   //slow down serial monitor for testing
 
  pirState = digitalRead(pir);

  if (pirState == HIGH && (hour, DEC) >= 18) {     
    digitalWrite(light, HIGH); 
    on = millis();
  }

  unsigned long currentMillis = millis();

  if ((currentMillis - on) > 30000) {
    digitalWrite(light, LOW);
  }
 
}


 
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (pirState == HIGH && (hour, DEC) >= 18
The ", DEC" doesn't do what you think, or want.
If hour is stored as BCD, then simply
Code:
if (pirState == HIGH && hour >= 0x18
should do.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think it should be 0x12? 18 in hex is 0x12, this works smiley
Thanks for the help.
 
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

18 is the same as 0x12, no need for any conversion.
Sorry, I thought your representation was BCD.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: