Pages: [1]   Go Down
Author Topic: Start/stop button  (Read 551 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again,

So I am trying to add a start/stop timer feature to a project I am working on. My problem is, I can not figure out how to properly program the button. I got the counting down and it displays fine on the lcd. But when I try to make it so all I have to do is press the button to start the counting, it will count once and then stop. I need help coming up with the loop to keep the timer going, and then to stop it once i press the button again. Thank you in advance.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hard to help out without seeing a code listing.
Use the # button when you post it.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry about that.

Here is everything so far, I know it's kinda messy but what i am asking for help with is the first block in the void loop

There is no control statement now. I deleted it trying to find something that would work
Code:

/*
Setting up start stop button
 */




#include <LiquidCrystal.h>
#include <Time.h>

time_t elapsedTime;

// initialize lcd
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

// in temp pins and variables
const int temp1Pin = A0;
const int temp2Pin = A1;
int temp1Reading;
int temp2Reading;
//int start/stop button and variables
const int timeButtonPin = 8;
int timerButtonState;
int lastTimerState = 0;
int sCount = 0;
int mCount = 0;
// int airflow buttons and variables for counting
const int airflowButton = 9;
int airflowCounter = 0;
int airflowButtonState = 0;
int lastAirflowState = 0;

void setup(void) {

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,1);
  lcd.print("Time:");
  lcd.setCursor(11,1);
  lcd.print("Air:");
  //define pinmodes
  pinMode(timeButtonPin, INPUT);
  pinMode(airflowButton, INPUT);
}

void loop() {
  //Timer display
  timerButtonState = digitalRead(timeButtonPin);

    if (sCount>59){
      mCount++;
      sCount=0;
      delay(58);
    }
    if (sCount<60){
      delay(988);
      sCount++;
    }
 
     lcd.setCursor(0,1);
      lcd.print("Time:");
      lcd.print(mCount);
      lcd.print(":");
      lcd.print(sCount);
      lcd.print(" ");

   
   
     
  //count airflow
  airflowButtonState = digitalRead(airflowButton);
  if (airflowButtonState != lastAirflowState){
    if (airflowButtonState == HIGH){
      airflowCounter++;
      lcd.setCursor(15,1);
      lcd.print(airflowCounter);
    }
  }
  lastAirflowState = airflowButtonState;
 
  //display temps
  lcd.setCursor(0,0);
  lcd.print("T1:");
  lcd.setCursor(8,0);
  lcd.print("T2:");
  temp1Reading = analogRead(temp1Pin);
  temp2Reading = analogRead(temp2Pin);
  int c1 = ( 5.0 * temp1Reading * 100.0) / 1024.0;
  int c2 = ( 5.0 * temp2Reading * 100.0) / 1024.0;
  int f1 = (((c1 * 9.0) / 5.0) + 32.0);
  lcd.setCursor(3,0);
  lcd.print(f1);
  int f2 = (((c2 * 9.0) / 5.0) + 32.0);
  lcd.setCursor(11,0);
  lcd.print(f2);
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45956
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My problem is, I can not figure out how to properly program the button.
I think you need to use a separate programmer for that.

Now, programming an Arduino to deal with a switch is a different story.

Quote
I know it's kinda messy
Messy is OK. You can use Tools + Auto format to correct that. Miles long is another matter.

Dump that sketch. Write some code that does nothing but read the switch(es), and turn on or off some LEDs to get a sense of what the switch is doing. Once you understand the basics, applying them to your program will be much easier.

For instance, you are not using the internal pullup resistors. This means that you need external pullup or pulldown resistors. Do you have them? How ARE the switches wired?

When you say "it didn't work", you aren't giving us anything to go on. "I expected this or that to happen. Instead, what happened was..." is much more useful information.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes I am using external resistors. And the reason it is miles long, is because it is part of a roast logger I am working on. But thank you for your pointers and i will work on trying to do it with just  an led or something and once I figure it out, incorporate it into my logger. Thank you
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright I redid the code so it is cleaner and shorter. But still having some trouble. I can have found out how to get the start aspect working. When I press the button the timer will turn on and start counting up by seconds. But when I can't not figure out how to make the timer stop where it is at when i press the same button again. I know i am stuck in a loop, but i can't think of a good exit plan
Code:
#include <LiquidCrystal.h>

#include <Time.h>


const int timeButton = 8;
const int airButton = 9;

int timeState;
int lastTimeState;
boolean timer = false;

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() {
  lcd.begin(20,4);
  mainScreen();
  pinMode(timeButton, INPUT);
  pinMode(airButton,INPUT); 
}

void loop() {
  timeState = digitalRead(timeButton);
  if(timeState != lastTimeState) {
    if(timeState == HIGH) {
      if(timer == true) timer = false;
      else timer = true;
    }
  lastTimeState = timeState;
  }
  while(timer == true) timeDisplay();
 
}
void mainScreen() {
  lcd.clear();
  lcd.home();
  lcd.print("T1:");
  lcd.setCursor(7,0);
  lcd.print("T2:");
  lcd.setCursor(0,1);
  lcd.print("Time:0:00");
  lcd.setCursor(11,1);
  lcd.print("AF:");
}

void timeDisplay() {
  lcd.setCursor(5,1);
  lcd.print(minute());
  lcd.setCursor(7,1);
  lcd.print(second());
  lcd.print(" ");
}
Logged

South Africa
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the code you have written is aimed at using a toggle switch instead of a push button.

With a toggle switch, you would look for a change that is different to the last value, as you have with :
Code:
if(timeState != lastTimeState) {

For a push button, you need to toggle the Boolean variable every time the button is pushed. I would use something like this :

WARNING : code not tested and most likely not the best way to write it, but it should give you the idea :

Code:
const int timeButton = 8;

int ButtonState = 0;
boolean ShowTimer = false;

void setup() {
  pinMode(timeButton, INPUT);
  digitalWrite(timeButton, HIGH);
  //enables the internal pull-up.
  //Button should connect Ground to input pin 8
  //HIGH reading = button not pressed
  //LOW reading = botton pressed
}

void loop() {

  ButtonState = digitalRead(timeButton);

  if(ButtonState == 0 then) {  // button has been pressed
if(ShowTimer == true) ShowTimer = false;  //switch the ShowTimer value
else ShowTimer = true;
delay(750);  //add a delay so that the state does not keep changing until the button is released
}

  if(ShowTimer == true){
// do what you want done when the ShowTimer is True 
  }
 
  if(ShowTimer == false){
// do what you want done when the ShowTimer is False 
  }
 
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you DaveO. The other day I was thinking about writing the switch to the pull up state, but I forgot to go with it when I finally got to my computer to start coding. Once i get a chance I will use your input to work with! Thank you for helping me, and for not just giving me working code, but for giving me code that need works! It will help me in learning to be a better programmer!
Logged

Pages: [1]   Go Up
Jump to: