Hi there fellow Arduino'ers;
I'm building an alarm clock with radio (RDA5807) that disables when you put your both feet on the carpet.
But I'm still in the beginning, so that was just some background info.
I currently have the following code which just displays hour, time and temperature (from DS3231) on the lcd without any problem.
#include <Wire.h>
#include <LiquidCrystal.h>
//lcd
LiquidCrystal lcd(12, 11, 5, 6, 7, 8);
byte tempC[8] = {
0b01100,
0b10010,
0b10010,
0b01100,
0b00000,
0b00000,
0b00000,
0b00000
};
byte tempChar[8] = {
0b00100,
0b01010,
0b01010,
0b01010,
0b01110,
0b11111,
0b11111,
0b01110
};
//DS3231
#define DS3231_I2C_ADDRESS 0x68
byte time_array[7];
//Rotary encoder
#define B_SIG 4
int pulses=0;
void setup()
{
Wire.begin();
Serial.begin(9600);
//lcd
lcd.begin(16,2);
lcd.createChar(0, tempC);
lcd.createChar(1,tempChar);
//rotary encoder
attachInterrupt(1, A_RISE, RISING);
pinMode(B_SIG,INPUT);
}
void loop()
{
Serial.println(pulses);
displayHome();
delay(50);
}
/*DS3231*/
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void readTime()
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
for(int i = 1; i < 8; i++)
{
time_array[i] = bcdToDec(Wire.read());
}
}
void displayHome()
{
readTime();
lcd.setCursor(0,0);
lcd.print(time_array[3], DEC);
lcd.print(":");
if (time_array[2]<10)
{
lcd.print("0");
}
lcd.print(time_array[2], DEC);
lcd.print(":");
if (time_array[1]<10)
{
lcd.print("0");
}
lcd.print(time_array[1], DEC);
lcd.setCursor(11,0);
lcd.write(1);
lcd.print(getTemp(),DEC);
lcd.write(byte(0));
lcd.print("C");
lcd.setCursor(0,1);
switch(time_array[4]){
case 1:
lcd.print("Zon");
break;
case 2:
lcd.print("Ma");
break;
case 3:
lcd.print("Di");
break;
case 4:
lcd.print("Woe");
break;
case 5:
lcd.print("Do");
break;
case 6:
lcd.print("Vr");
break;
case 7:
lcd.print("Zat");
break;
}
lcd.print(" ");
lcd.print(time_array[5], DEC);
lcd.print("/");
lcd.print(time_array[6], DEC);
lcd.print("/");
lcd.print(time_array[7], DEC);
}
int getTemp()
{
int temp;
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS,2);
delay(15);
byte MSB = Wire.read();
byte LSB = Wire.read()>>6;
if ((MSB & 0x80) != 0)
temp = MSB | ~((1 << 8) - 1); // if negative get two's complement
else
temp = MSB;
temp = 0.25 * LSB + temp;
return temp;
}
/*Rotary Encoder*/
void A_RISE(){
detachInterrupt(1);
boolean var = digitalRead(B_SIG);
if(var==0)
pulses++;//moving forward
if(var==1)
pulses--;//moving reverse
attachInterrupt(1, A_RISE, RISING);
}
I wanted to add my rotary encoder, for starters just by showing the steps it was turned in the Serial monitor.
Result:
when I comment out displayHome() in the loop, everything works fine.
When I leave it in the loop, it prints by default 15 in the Serial monitor. When I turn the rotary encoder, I can see that the right amount of steps is added and then the in the following cyclus the value is 15 again.
I really can't imagine how this can happen. I'm not even using the variable 'pulses' in the displayHome() code....
Thank you for reading this and trying to help ![]()
Bram