Go Down

Topic: Bytes to decimals to an integer? (Read 280 times) previous topic - next topic

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: [Select]

/*
  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);
  }
 
}



www.bajdi.com

AWOL

Code: [Select]
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: [Select]
if (pirState == HIGH && hour >= 0x18 should do.
"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.

I think it should be 0x12? 18 in hex is 0x12, this works :)
Thanks for the help.
www.bajdi.com

AWOL

18 is the same as 0x12, no need for any conversion.
Sorry, I thought your representation was BCD.
"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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy