Hi guys,
I'm James
I decided to make a binary clock - you know how they are.. i based it from my watch and thought i had it sussed. Unfortunately, i did not....... As far as i could tell, it was going to work.. but yeah, help please!!
I've included the code below, its got 2 functions for the display in binary
(hour and minute) and the main loop to count! There are comments to explain it as i go but if anyone needs to ask, go ahead and i'll try explain my thought process! I have a line of LEDs on a breadboard ready as a display. I'd prefer not to look at any examples or anything if i can help it! If anyone can help me rescue this I'd be very appreciative! But otherwise I'll start afresh and work through slower.. Its just puzzling why it doesn't work, that's all
Cheers everyone!
James
// James Leyland 27-12-16
//variables - need to be global?
//including attempt of display
//the two functions in theory work for decoding the counter into binary, but not tested yet.
//source of likely error is the IF STATEMENT involving the hourCount and tempHour (i tried to make it so it doesnt have to work out the hour output every minute!)
//WRITTEN WITH NO REFERENCES OR EXAMPLES!!!!!!! (quite proud despite the major defect of it not working -_-)
int minOne = 4;
int minTwo = 5;
int minFour = 6;
int minEight = 7;
int minSixteen = 8;
int minThirtyTwo = 9;
int hourOne = 0;
int hourTwo = 1;
int hourFour = 2;
int hourEight = 3;
int AMLED = 10;
int PMLED = 11;
int sec = 12;
//counters
int secCount = 0;
int minCount = 0;
int hourCount = 0;
int AM = 0;
int PM = 0;
void setup() {
pinMode(minOne, OUTPUT);
pinMode(minTwo, OUTPUT);
pinMode(minFour, OUTPUT);
pinMode(minEight, OUTPUT);
pinMode(minSixteen, OUTPUT);
pinMode(minThirtyTwo, OUTPUT);
pinMode(hourOne, OUTPUT);
pinMode(hourTwo, OUTPUT);
pinMode(hourFour, OUTPUT);
pinMode(hourEight, OUTPUT);
pinMode(AMLED, OUTPUT);
pinMode(PMLED, OUTPUT);
pinMode(sec, OUTPUT);
}
void loop() {
while (1);
{
int tempHour; //may be a cause of error if it doesnt work. temphour is to reduce the amount of times that the hour loop is done; only if the hourCount changes
minutes(minCount);
if (hourCount =! tempHour)
{
hours(hourCount);
}
tempHour = hourCount;
delay(800);
digitalWrite(sec, HIGH);
delay(198); //allowing 1ms for the rest of the loop to run
digitalWrite(sec, LOW);
secCount = secCount + 1;
if (secCount = 60)
{
secCount = 0;
minCount = minCount + 1; //concern expressed about the nested ifs. will this work? do they need to be after one another?
if (minCount = 60)
{
minCount = 0;
hourCount = hourCount + 1;
if (hourCount = 12)
{
hourCount = 0;
if (AM = 1)
{
AM = 0;
PM = 1;
digitalWrite(AMLED, LOW);
digitalWrite(PMLED, HIGH);
}
else
{
AM = 1;
PM = 0;
digitalWrite(PMLED, LOW);
digitalWrite(AMLED, HIGH);
}
}
}
}
}
}
void minutes(int count)
{
digitalWrite(minOne, LOW);
digitalWrite(minTwo, LOW);
digitalWrite(minFour, LOW);
digitalWrite(minEight, LOW);
digitalWrite(minSixteen, LOW);
digitalWrite(minThirtyTwo, LOW);
if (count > 32)
{
count = count - 32;
digitalWrite(minThirtyTwo, HIGH);
}
if (count > 16)
{
count = count - 16;
digitalWrite(minSixteen, HIGH);
}
if (count > 8)
{
count = count - 8;
digitalWrite(minEight, HIGH);
}
if (count > 4)
{
count = count - 4;
digitalWrite(minFour, HIGH);
}
if (count > 2)
{
count = count - 2;
digitalWrite(minTwo, HIGH);
}
if (count = 1)
{
count = count - 1;
digitalWrite(minOne, HIGH);
}
if (count != 0)
{
//error - outputs 63
digitalWrite(minOne, HIGH);
digitalWrite(minTwo, HIGH);
digitalWrite(minFour, HIGH);
digitalWrite(minEight, HIGH);
digitalWrite(minSixteen, HIGH);
digitalWrite(minThirtyTwo, HIGH);
}
return;
}
void hours(int count)
{
digitalWrite(hourOne, LOW);
digitalWrite(hourTwo, LOW);
digitalWrite(hourFour, LOW);
digitalWrite(hourEight, LOW);
if (count > 8)
{
count = count - 8;
digitalWrite(hourEight, HIGH);
}
if (count > 4)
{
count = count - 4;
digitalWrite(hourFour, HIGH);
}
if (count > 2)
{
count = count - 2;
digitalWrite(hourTwo, HIGH);
}
if (count = 1)
{
count = count - 1;
digitalWrite(hourOne, HIGH);
}
if (count != 0)
{
//error - outputs 15
digitalWrite(hourOne, HIGH);
digitalWrite(hourTwo, HIGH);
digitalWrite(hourFour, HIGH);
digitalWrite(hourEight, HIGH);
}
return;
}