Problem with a simple code, i guess...

Code, as said, is very simple. I have a picture of my schematics also made with Eagle. My 3 buttons work as planned (start stopwatch, stop countdown, take a laptime), but when combining them with my 2 optical sensors, issues emerge. I can start the stopwatch with sensor, but after the time is running i feel like sensors are dead or not giving any voltages/analogvalues anymore. Do i have to reset analogRead or something ? Also did some “debugging” with serial monitor, but readings were “stopped” after stopwatch was running. Here’s the code:

#include <LiquidCrystal.h>

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

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();

  pinMode(8, INPUT);            //start
  digitalWrite(8, HIGH);
  pinMode(9, INPUT);            //stop
  digitalWrite(9, HIGH);
  pinMode(10, INPUT);
  digitalWrite(10, HIGH);       //laptime
}


double seconds = 0;
double a = millis();
double c ;


void loop()
{
  int sensorStart  = analogRead(A5);
  float startVoltage = sensorStart * (5.0 / 1023.0);
 
  int sensorStop   = analogRead(A0);
  float stopVoltage  = sensorStop * (5.0 / 1023.0);

  lcd.setCursor(0,0);
  lcd.print("press start");                                   


    if(digitalRead(8) == LOW || sensorStart > 200 )          // start button is pressed or a reading from start sensor..
    {
      lcd.clear();
      a = millis();
    
      while(digitalRead(9) == HIGH )                           // taking time, loop on..
        {
          c = millis();
          seconds = (c - a) / 1000;
          lcd.print(seconds);     
          lcd.setCursor(11,0);
          lcd.print("Sec's");
          lcd.setCursor(0,0);
          
            if(digitalRead(10) == LOW )                        //laptime printed 2nd row 
            {         
             lcd.setCursor(0,1);
             lcd.print(seconds);
             lcd.setCursor(9,1);
             lcd.print("Laptime");
            }
            
            if(digitalRead(9) == LOW )                         // stop button is pressed...
            {
              while(digitalRead(8) == HIGH )                   // while taking time...
              {
                 lcd.setCursor(0,0);
                 lcd.print(seconds);
                 lcd.setCursor(11,0);
                 lcd.print("Sec's");
                 lcd.setCursor(0,0);
              }
            }         
         }
      }           
}

And here are the schematics if u are interested. I’ve not seen any smoke…

I’d be so thankful if u take a look at it.

  pinMode(8, INPUT);            //start
  digitalWrite(8, HIGH);
  pinMode(9, INPUT);            //stop
  digitalWrite(9, HIGH);
  pinMode(10, INPUT);
  digitalWrite(10, HIGH);       //laptime

Use INPUT_PULLUP and get rid of the digitalWrite()s, and your intent will be much clearer.

double a = millis();

millis() returns an integral value. It makes no sense to store that in a floating point type.

Naming the pins, so there is some clue why you are reading a particular pin would be really helpful.

I can see no reason to convert the analogRead() values to voltages. Nor do I see anywhere where you read the analog pins again in the while loops, once the first if statement is true.

thanks Paul for the reply. I Fixed what i could. Here’s the working code if any of u are interested… issues were in the int placement…

#include <LiquidCrystal.h>

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

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();

  pinMode(8, INPUT_PULLUP);            //start
  pinMode(9, INPUT_PULLUP);            //stop
  pinMode(10, INPUT_PULLUP);          //laptime

}

double seconds = 0;
unsigned long a = millis();
double c ;


void loop()
{
  int sensorStart  = analogRead(A5);  
  int btnStart = digitalRead(8) == LOW;

//  int btnStop = digitalRead(9) == LOW;
//  int btnLaptime = digitalRead(10) == LOW;
//  int vittu = digitalRead(9) == HIGH;
  
  lcd.setCursor(2,0);
  lcd.print("Press Start");                                   


    if( btnStart || sensorStart > 200) // start button is pressed or a reading from start sensor..
    {
      lcd.clear();
      a = millis();
    
      while(digitalRead(9) == HIGH )  // taking time, loop on..
        {
          int sensorLaptime   = analogRead(A0);             // <----- analogread loopin sisälle, että sitä luetaan kokoajan while loopin käydessä
          int btnLaptime = digitalRead(10) == LOW;
                 
          c = millis();
          seconds = (c - a) / 1000;

          lcd.setCursor(0,0);
          lcd.print(seconds);     
          lcd.setCursor(11,0);
          lcd.print("Sec's");
          lcd.setCursor(0,0);
          
            if( btnLaptime || sensorLaptime > 200 )  //laptime from button or from sensor2
            {                 
             lcd.setCursor(0,1);
             lcd.print(seconds);
             lcd.setCursor(9,1);
             lcd.print("Laptime");
             lcd.setCursor(0,0);
             delay(500);
            }

            int btnStop = digitalRead(9) == LOW;
            
            if( btnStop )  // stop button is pressed... 
            {
                 lcd.clear();
                 lcd.setCursor(0,1);
                 lcd.print(seconds);
                 lcd.setCursor(8,1);
                 lcd.print("End Time");         
            }         
         }
      }           
}

c = millis(); millis does not return a double.

I don’t quite get this i’m afraid… if i change double to a long / unsigned long, my stopwatch doens’t show milliseconds anymore on my lcd display which is pretty important ? Also, since obviously i’m bad with millis(); function, how do i add minutes to this calculation ? I’ve tried all i can… Current code is here with all the suggestions told… sensors work etc.

#include <LiquidCrystal.h>

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

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();

  pinMode(8, INPUT_PULLUP);            //start
  pinMode(9, INPUT_PULLUP);            //stop
  pinMode(10, INPUT_PULLUP);          //laptime

}

double seconds = 0;
unsigned long a = millis();
double c ;


void loop()
{
  int sensorStart  = analogRead(A5);  
  int btnStart = digitalRead(8) == LOW;
  
  lcd.setCursor(2,0);
  lcd.print("Press Start");                                   


    if( btnStart || sensorStart > 200) // start button is pressed or a reading from start sensor..
    {
      lcd.clear();
      a = millis();
    
      while(digitalRead(9) == HIGH )  // taking time, loop on..
        {           
          c = millis();
          seconds = (c - a) / 1000;
          lcd.setCursor(0,0);
          lcd.print(seconds);     
          lcd.setCursor(11,0);
          lcd.print("Sec's");
          lcd.setCursor(0,0);            

          int btnLaptime      = digitalRead(10) == LOW;
          int sensorLaptime   = analogRead(A0);             //<-- Reading analogValue during loop
          int btnStop = digitalRead(9) == LOW;                       
            
            if( btnLaptime || sensorLaptime > 200 )  //laptime from button or from sensor2
            {                 
             lcd.setCursor(0,1);
             lcd.print(seconds);
             lcd.setCursor(9,1);
             lcd.print("Laptime");
             lcd.setCursor(0,0);
             delay(500);
            }
                                  
            if( btnStop )  // stop button is pressed... 
            {
              lcd.clear();
              lcd.setCursor(0,1);
              lcd.print(seconds);
              lcd.setCursor(8,1);
              lcd.print("End Time");         
            }         
         }
      }           
}
      a = millis();
          c = millis();

It would be MUCH better to use names that reflected WHY you cared what millis() returned at these points.

It would be better to use the proper type to hold the integral value returned.

          seconds = (c - a) / 1000;

A millisecond is NOT 1/1000th of a second. A second is 0.001 milliseconds. This is important, because it means that a millisecond is 1/1000.0 seconds. If you don’t think that there is a difference, you are wrong.

Also, multiplication is much faster than division.

PaulS:
A second is 0.001 milliseconds.

Excuse me ??

Erik_Baas:
Excuse me ??

What? You never made a typo?

You're excused.