The impossible stopwatch, at least for me :(

Hi, hope some one can help me out trying to combine this two programs, I´m trying to make a stopwatch but the start buttons instead of being buttons I want to make them with FSR sensors,
I have already a program that works as a stopwatch but I need to change it to use the FSR and also because on the start is a pullup button (wich is good) but the stop is also a pullup button and while you keep pressing the time keeps running, and it should be a press button to just stop.

The First piece of code is the program that I use for the stopwatch, and the second is the program I use for the FSR sensor.

The goal is to combine the two programs, modify the stopwatch so instead of using regular buttons, use the FSR sensors, and make the start based on Pull up (so while you are standing the timer is not working) and the stop make it a press button but also with FSR, two different FSR that work differently.

Thanks in Advance, here is the code for the stopwatch

// StopWatch trying to change the way it starts and stops but using FSR 

#include "LedControl.h"
LedControl lc = LedControl(12, 11, 10, 1);//Set up the Max7219 display pins
unsigned long previousMillis = 0;
unsigned long interval = 6;
const int buzzer = 6;// Defines the pin for the buzzer
byte estado = 0;// variable to avoid re-start of the timer before the other is on the race

byte pinsBut [] = { 7, 8, 9 };// Array for the start and finish button, Problem I have to change with FSR
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

bool          run   = false; //States if the timmer is running or not
unsigned long msec0 = 0;

//-----------------------------------------------------------
//Here I put the variables for the FSR Sensors
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 4;      // connect Red LED to pin 11 (PWM pin)to check if activates or not
int fsrReading;      // the analog reading from the FSR resistor divider
int LEDbrightness;

// -----------------------------------------------------------------------------
int chkButtons ()
{
  for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
    byte but = digitalRead (pinsBut [n]);//Reads the state for the pins that are installed in the buttons

    if (butState [n] != but)  {
      butState [n] = but;

      delay (10);     // debounce

      if (HIGH == but)// Problem here is that for the start is better to have it as High so when you stop touching the timer starts but for the stop if you keep touching the timer keeps going 
      
        return n;
    }
  }
  return -1;
}

// -----------------------------------------------------------------------------
char *//Calculates the functions that are shown in the Serial monitor and in the timer
fmtTime (
  unsigned long msec )
{
  static char s [80];

  unsigned      mins = msec / (60L * 1000);
  unsigned      hour = mins / 60;
  mins = mins % 60;
  unsigned      secs = msec % (60L * 1000);

  sprintf (s, "  %2d:%02d:%02d.%03d",
           hour, mins, secs / 1000, secs % 1000);
  return s;

}

// -----------------------------------------------------------------------------
void loop ()
{
  unsigned long msec = millis ();
  unsigned      mins = msec / (60L * 1000);// for the timing data
  unsigned      hour = mins / 60;
  mins = mins % 60;
  unsigned      secs = msec % (60L * 1000);
// Checks the FSR sensor 
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  if (run)  {
    estado = 1;// This is what stops from starting again while the person is running
    unsigned long time = msec - msec0;
    Serial.println (fmtTime (time));//Display the data in the serial monitor
    int ones, tens, fsec, sec, minute, reminuto, dsec;
    // First display fsec
    fsec = time / 10;
    ones = time / 10 % 10; // hundreds of a second
    tens = (fsec / 10) % 10; //Tens of a second
    lc.setDigit(0, 0, (byte)ones, false); //Shows the Hundreds and is correct
    lc.setDigit(0, 1, (byte)tens, false); //Shows the Tens and is correct
    // Now display sec
    lc.setChar(0, 2, '.', false);//Puts the dot in the middle of the 7216 Display
    lc.setChar(0, 5, '.', false);//Puts the other dot in the middle of the 7216 Display
    
    
    sec = (time / 1000) % 10;
    dsec = (time / 10000) % 6;
    
    lc.setDigit(0, 4, (byte)dsec, false);//Shows seconds
    lc.setDigit(0, 3, (byte)sec, false);

    // Next display mm
    ones = (time / 60000) & 59; //turns into a minute
    tens = (time / 600000);
    lc.setDigit(0, 7, (byte)tens, false); //shows 2 minutes
    lc.setDigit(0, 6, (byte)ones, false); // shows seconds
  }

  switch (chkButtons ())  {
    case 1:     // Stop case, the timmer is not running
      run = false;
      estado = 0;//to be sure it does not start again
      tone(buzzer, 200, 250);// makes noise when stops
      digitalWrite(5, LOW);// Turns off a light when is not running
      digitalWrite(4, HIGH); // Turns On a light when the timer si running
      break;



    case 0:     // start, The stop watch is running
      if(run   == true and estado == 1) {//I make sure that it has stopped before starts again
      break;
      } else if(estado == 1);{
      run = true;
      tone(buzzer, 850, 350);//Makes noise when starts
      digitalWrite(5, HIGH);// The led that indicates that its running starts 
      digitalWrite(4, LOW);// The ready light is turned off
      
      msec0 = msec ; 
      digitalWrite(5, HIGH);// I think this is a mistake
      digitalWrite(4, LOW);}// I think this is a mistake because its already turned on..
     
      
      break;
  }
}

// -----------------------------------------------------------------------------
void setup ()
{
  Serial.begin (9600);
  pinMode(buzzer, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  // the zero refers to the MAX7219 number, it is zero for 1 chip
//-----------------------
  pinMode(LEDpin, OUTPUT);//Pin for the led of the FSR
  lc.shutdown(0, false); // turn off power saving, enables display
  lc.setIntensity(0, 12); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
  int fsec, sec, minute, reminuto;// declares the time variables

  for (unsigned n = 0; n < sizeof(pinsBut); n++)  {//Checks the pin variables in case there are changes
    pinMode (pinsBut [n], INPUT_PULLUP);
    butState [n] = digitalRead (pinsBut [n]);
  }

}

________________________________ Code for the FSR sensors__________

//* FSR testing sketch. 
 
//Connect one end of FSR to 5V, the other end to Analog 0, and the second FSR to Analog 1
//Then connect one end of a 10K resistor from Analog 0 to ground
//Connect LED from pin 11 through a resistor to ground 
 
int fsrAnalogPin = 0; // FSR is connected to analog 0 for to work as a button for start, when stops having preasure
int fsrAnalogPin2 = 1; // FSR is connected to analog 1 for stop when it feels pressure.
int fsrReading;      // the analog reading from the FSR resistor divider
int fsrReading2;      // the analog reading from the FSR resistor divider
int FSRpad;//store the value of the start sensor
int FSRpad2;// store the value for the stop sensor
const int buzzer = 6;
void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
  pinMode(buzzer, OUTPUT);
}
 
void loop(void) {
  fsrReading = analogRead(fsrAnalogPin); 
  fsrReading2 = analogRead(fsrAnalogPin2); 
 
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
  Serial.println(fsrReading2);
  // we'll need to change the range from the analog reading (0-1023) down to the range
  // used by analogWrite (0-255) with map!
  FSRpad = map(fsrReading, 0, 1023, 0, 255);
  FSRpad2 = map(fsrReading2, 0, 1023, 0, 255);
  if (FSRpad > 100) {
    tone(buzzer, 200,300);// means the runner has started
  }
    if (FSRpad2 > 400) {
    tone(buzzer, 200,500);// means the runner should have stopped
  }
 
  delay(1);
}

Which Arduino are you using?

If an UNO, and you want to get the analog channel zero value you specify A0.  What you've declared takes an analog reading from pin 0, part of the serial communication port.

No.
The value zero passed to analogRead reads pin A0.

I'm using the UNO model, just changed to A0 instead of 0 and looks that it works faster.

Hi, I don't know what is the difference _

I'm using a UNO. Thanks

Well I'll be.   Apologies for the erroneous information.  :grimacing:

int fsrAnalogPin = 0; 
int fsrAnalogPin2 = 1;

But . . . this does self document.

int fsrAnalogPin = A0; 
int fsrAnalogPin2 = A1;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.