I'm new to programming and I am trying to make a clock that will wait for the first 10 seconds to set an alarm, using two buttons (one as a one and the other button as a zero for binary). The clock works fine right now it is setting an alarm I'm having trouble with. Any help will be appreciated. Thanks
OK, it's your first program after Blink so I'll cut you some slack. There's a lot of poor programming practices there which are so bad it's not actually wrong, so let's fix the parts that are wrong.
What do you think this does?
int alarmhour = (sethour, BIN);
Inside the Arduino, all the variables are binary. You don't convert them to binary. That expression you've copied from a print() statement is only a format used for output on a screen.
Open up the calculator app on your computer. If it's a recent version of Windows, it has a "programmer" mode that shows the binary representation of the numbers you are typing in. I'm guessing other operating systems have a similar calculator. Type a "4" - what is the binary representation of 4? Type a "16" - what does it show? What do you think it will look like if you add a 4 and a 16?
The Arduino provides some simple macros to help you here. If you want to set the 3rd bit of a variable, you don't need to remember that this is equal to 4. You use the bit(2) macro to set the 3rd bit. [bit(0) is the first bit. Computers count from zero.]
alarmhour = bit(2) + bit(4); //this is identical to 4+16
There's lots of other ways to write that same expression. It can be spread over multiple lines or multiple iterations of a loop:
int alarmhour = 0;
for(int bitnum = 2; bitnum <= 4; bitnum += 2) {
alarmhour |= bit(bitnum);
}
Serial.print("Decimal representation: ");
Serial.println(alarmhour, DEC);
Serial.print("Binary representation: ");
Serial.println(alarmhour, BIN); //note this doesn't print leading zeroes like you might expect
Hey I changed around my code, hopefully this is clearer, however my alarm still isnt working.
/*
Name: ARDUINO CLOCK
Version 1.0
Friday, November 20th, 2015
Atmega 328p
Purpose:
This clock will display the time by lighting up LEDs in a pattern to show the time.
The LEDs will be on to show the hours for 5 seconds, then it will flash the number of minutes for 5 seconds.
This clock is working on military time.
*/
//Constants and Variables
int hourstart = B00001011; //Sets the beginning hour to 11
int minutestart = B00101000; //Sets the beginning minute to 40
int off = B00000000; //Turns all the LEDs off
byte x;
byte y;
int delay1 = 4500;
int delay2 = 500;
byte delay3 = 250;
int z;
byte w;
byte t;
byte IN1 = 5;
byte IN2 = 6;
byte Read1;
byte Read2;
byte alarmhour = 0;
byte alarmminute = 0;
void setup() {
DDRB = B00111111; //Sets my required pins to outputs
PORTB = B00111111; //Turns all pins on to ensure they work.
delay (500);
PORTB = off; //Turns all pins off
delay (500);
pinMode (IN1, INPUT);
pinMode (IN2, INPUT);
pinMode (7, OUTPUT);
alarmset();
}
void loop() {
for (w = 11; w < 24; w++) { //Counts hours in day (24)
for (y = 40; y <= 59; y ++) { //Counts the minutes in an hour (60)
for (x = 1; x <= 6; x++) { //Counts seconds in an minute (60)
PORTB = hourstart; //Displays the number of hours
delay (delay1); //Delays the next line of code for 4.5 seconds
PORTB = off; //Turns all leds off
delay (delay2);//Delays next line of code for 0.5 seconds
flash(); //Flashes the LEDs to display minutes
PORTB = off;
delay (delay2);
if (x == 6) { //Does following commands every minute
minutestart = minutestart + B00000001; //Adds one minute to time
y = y + 1; //Increases minute counter by one
x = 1; //Resets the seconds back to zero
}
if (y == 60) { //Does the following commands every hour
hourstart = hourstart + B0000001; //Increases hour count by one
minutestart = B00000000; //Resets minutes back to zero
y = 0; //resets minute count to one
w = w+1; //Increases hour count by one
}
if (w == 24) { //Does the following commands every 24 hours
w = 1; //Resets the hour count to one
hourstart = B00000001; //resets hour display to one
}
if (alarmhour == hourstart && alarmminute == minutestart) {
alarm();
}
}
}
}
}
void flash() {
for (z = 1; z < 10; z++) {//Repeats following code 10 times (for 4.5 seconds)
PORTB = minutestart;//Displays number of minutes
delay (delay3);//delays next tine of code for 0.25 seconds
PORTB = off;//Turns off the leds
delay (delay3);
}}
void alarmset() {
for (z = 0; z < 249; z ++){
delay (20);
scan();
delay(20);
if (Read1 == HIGH && Read2 == HIGH){
PORTB = B00111111;
delay (1000);
PORTB = off;
delay (1000);
for (byte s = 1; s < 8; s++) {
scan();
if (Read1 == HIGH && Read2 == LOW) {
alarmhour = alarmhour + (2 * s);
}
if (Read1 == LOW && Read2 == HIGH) {
alarmhour = alarmhour + 0;
}
else {
s = s - 1;
}
}
String hourtimer = String (alarmhour, BIN);
for (byte q = 1; q < 8; q++) {
scan();
if (Read1 == HIGH && Read2 == LOW) {
alarmminute = alarmminute + (2 * q);
}
if (Read1 == LOW && Read2 == HIGH) {
alarmminute = alarmminute + 0;
}
else {
q = q - 1;
}
}
String minutetimer = String (alarmminute, BIN);
}
}
}
void scan() {
Read1 = digitalRead(IN1);
Read2 = digitalRead (IN2);
}
void alarm() {
for (byte n = 0; n < 9; n ++){
digitalWrite (7, HIGH);
delay (250);
digitalWrite (7, LOW);
delay (250);
}
}
Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags which make the code looklike thiswhen posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.
void alarm() {
for (byte n = 0; n < 9; n ++){
digitalWrite (7, HIGH);
delay (250);
digitalWrite (7, LOW);
delay (250);
}
}
The braces (}) have to line up. It's unreadable in this form.
Use the auto-formatter and see what it changes. It will make simple mistakes obvious.
In your alarmset() function, you use a for() loop with an increment (s++) but then you modify the value of s inside the loop. That's usually a bad idea. It makes it difficult to follow the logic of the loop. I would suggest a while() loop and you can modify the counter variable however you want within the loop.