Pages: [1]   Go Down
Author Topic: I don't understand why this won't work???  (Read 375 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley


Code:
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;
}
« Last Edit: December 03, 2012, 10:43:58 pm by santoroma17 » Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you dont call CheckTimeButtons() in the main loop that I can see
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley heres my origanal reply:

"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."
« Last Edit: December 04, 2012, 10:43:23 am by santoroma17 » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2395
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: