Go Down

Topic: Trying to use the on board push buttons (Read 3284 times) previous topic - next topic

Malzx

Nov 17, 2011, 03:50 am Last Edit: Nov 17, 2011, 03:58 am by Malzx Reason: 1
For my project I am building a thermostat using the Romeo All In One board and current trying to get it to where we can increment or decrease a set temperature using the on board buttons, while showing the actual current temperature, but they way we have coded it the set temperature is constantly decreasing or increase any help or suggestion would be appreciated.

Code: [Select]
#include <LiquidCrystal.h>

int pin = 0; //Initialize analog pin
int tempc = 0,tempf=0; //Temperature variables
int samples[1]; //Variables to make a better precision
int maxi = -100,mini = 100; //To start max/min temperature
int i;
int set = 70;                               //   
int key_s6 = 2;                                  //   
int key_s7 = 3;


//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 9, 10, 11, 12);
int brightness = 255;

void setup() {
 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.

  Serial.begin( 9600 );

  pinMode(key_s6, INPUT);       // 
  pinMode(key_s7, INPUT);

}

void loop() {
 
   if(digitalRead(key_s6)== LOW)    //
          {
           set--;   // 
          }
   if(digitalRead(key_s7)== LOW)   //
          {
           set++;    // 
          }
   
for(i = 0;i<=7;i++){ //Gets 8 samples of temperature

  samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
  tempc = tempc + samples[i];
 
 
  tempc = tempc/1.0; //Better precision
  tempf = (tempc * 9)/ 5 + 32; //Converts to fahrenheit

  //if(tempc > maxi) {maxi = tempc;} // set max temperature
// if(tempc < mini) {mini = tempc;} // set min temperature
 
}
 
  lcd.print(set); //Prints temperature in Celsius
  lcd.print(" Set ");
 
  lcd.print(tempf); //Prints temperature in Fahrenheit
  lcd.print(" F ");
 
  lcd.setCursor(0,0); //Set location on display screen
 
  tempc = 0;
  delay(3000);
 
 

}

Malzx

Fixed the code the original wasn't our project.

nickgammon


... they way we have coded it the set temperature is constantly decreasing or increase any help or suggestion would be appreciated.


You mean it does that:


  • Without touching the buttons?

  • While you hold the button down, but stops if you let go?

  • Once you press the button it keeps increasing/decreasing even after you let go?

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Malzx


AWOL

Floating pins?
Are you using external pullups?

nickgammon

The Romeo All In One schematic appears to show 10K pullups for switches 6 and 7 which are connected to pins 2 and 3.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

This isn't so good:

Code: [Select]
int samples[1]; //Variables to make a better precision

...

for(i = 0;i<=7;i++){ //Gets 8 samples of temperature

    samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;


Out of curiosity, what do you think will happen when the variable i becomes greater than zero in that loop?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up