Go Down

Topic: I don't understand why this won't work??? (Read 397 times) previous topic - next topic

santoroma17

Dec 04, 2012, 04:41 am Last Edit: Dec 04, 2012, 04:43 am by santoroma17 Reason: 1
im trying to look at an input on pin 3 and if its HIGH then hour++

Its for a binary clock. I didn't show the render code because it works and it just adds clutter. Also i know its not neat, Im just testing. and im new :)


Code: [Select]
int second=0, minute=0, hour=1;
int mUnit = 0;
int hUnit = 0;
int sUnit = 0;
int TempReading;
int TempSensorPin = 0;
int TempSwitch = 1;
int HourUp = 3;
int MinuteUp = 2;
int FinalTemp10;
int Display = 1;
int FinalTemp;
int Column,Row;

int TimeSwitchVal = 0;

int Column6 = 13;
int Column5 = 12;
int Column4 = 11;
int Column3 = 10;
int Column2 = 9;
int Column1 = 8;

int Row1 = 7;
int Row2 = 6;
int Row4 = 5;
int Row8 = 4;

void setup() {
 Serial.begin(9600);
 pinMode(Column6, OUTPUT);
 pinMode(Column5, OUTPUT);
 pinMode(Column4, OUTPUT);
 pinMode(Column3, OUTPUT);
 pinMode(Column2, OUTPUT);
 pinMode(Column1, OUTPUT);

 pinMode(Row1, OUTPUT);
 pinMode(Row2, OUTPUT);
 pinMode(Row4, OUTPUT);
 pinMode(Row8, OUTPUT);
 pinMode(HourUp, INPUT);
 pinMode(2, INPUT);
 pinMode(1, INPUT);
 pinMode(0, INPUT);
}

void loop() {
 tick();
 RenderTime();
/*
 if(Display == 1){
   RenderTime();
 }
 if(Display == 2){
   RenderTemp();
 }
 */
}

void Reset(){
 delayMicroseconds(200);
 digitalWrite(Column6, HIGH);
 digitalWrite(Column5, HIGH);
 digitalWrite(Column4, HIGH);
 digitalWrite(Column3, HIGH);
 digitalWrite(Column2, HIGH);
 digitalWrite(Column1, HIGH);

 digitalWrite(Row1, LOW);
 digitalWrite(Row2, LOW);
 digitalWrite(Row4, LOW);
 digitalWrite(Row8, LOW);
}
void CheckTempButtons() {
 //TempSwitchVal = digitalRead(TempSwitch);
// if(digitalRead(TempSwitchVal) == HIGH)
 {
 //  Display = 2;
 }
//  else
 {
 //  Display = 1;
 }
}

void CheckTimeButtons() {
 TimeSwitchVal = digitalRead(HourUp);
 if(TimeSwitchVal == HIGH)
 {
   hour++;
 }
 else{
   hour++;
 }
}

void print(){
 Serial.print(hour);
 Serial.print(":");
 Serial.print(minute);
 Serial.print(":");
 Serial.println(second);
 Serial.print(FinalTemp);
 Serial.print(" degrees C");
 
 Serial.println(TimeSwitchVal);
}

void tick(){
 static unsigned long lastTick = 0;
 if (millis() - lastTick >= 1000) {
   lastTick = millis();
   second++;
   print();
   CalcTemp();
   CheckTempButtons();
 }
 sUnit = second%10;
 mUnit = minute%10;
 hUnit = hour%10;

 if (second >= 60) {
   minute++;
   second = 0;
 }

 if (minute >= 60) {
   hour++;
   minute = 0;
 }
 if (hour >= 24) {
   hour=1;
 }
}
void CalcTemp(){
 TempReading = analogRead(TempSensorPin);
 float voltage = TempReading * 5.0;
 voltage /= 1024.0;
 float TempC = (voltage - 0.5) * 100;
 FinalTemp = (int) TempC;
 FinalTemp10 = FinalTemp % 10;
}

Osgeld

you dont call CheckTimeButtons() in the main loop that I can see
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

santoroma17

#2
Dec 04, 2012, 04:40 pm Last Edit: Dec 04, 2012, 04:43 pm by santoroma17 Reason: 1
God im dumb i hade the check tempbutton code and timebutton code in a CheckButton funtion but i split it to see if that was the problem. ill fix it when i get home from school :) heres my origanal reply:

[font=Verdana]"hey Osgeld thanks for the reply. sadly its in the "Tick" funtion so it only checks the buttons once per second. it worked when i had the buttons on analog but if i presses the hour button then the hours would increase but if i pressed minute then both minutes and hours would increase. All i changes was the if statments so it works on digital. but now nothing happends when i press any buttons.. i dont get it."[/font]

holmes4

You also need to debounce your buttons, check in the playground for details.

Mark

santoroma17

Wouldn't having the arduino check the buttons once a second already be debounceing unless you reaction time is worse then one second?????

Jimmy60

Quote
Wouldn't having the arduino check the buttons once a second already be debounceing unless you reaction time is worse then one second?????


Not really but it will basically be relying on luck to detect a button press. It will only detect a button press if, coincidentally, the user presses the button at the same moment the Arduino checks it. At the very least the user will generally have to hold the button for at least one second to ensure a reading.

Read it every loop and debounce it. This will read it fast and often enough that it will seem like it is always checking the button.

Go Up