CSC270 Stopwatch

From CSclasswiki
Jump to: navigation, search


Stopwatchtestimage.png


Finite State Machine

Our first task was to create a Finite State Machine that would work as shown in the diagram below.

State diagram.png

We tested moving in between the state with an input from the button first, since it is easy to set up and control and poses a minimal risk of error (unlike the sound sensor).

#include <wiringPi.h>
#include <stdio.h>

#define ButtonPin 1

int main(void)
{
  wiringPiSetup();
  if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen
     printf("setup wiringPi failed !");
     return 1; 
  }
         
  pinMode(ButtonPin, INPUT);

  int state = 0;
  int last_button = 1; //not pressed

  pullUpDnControl(ButtonPin, PUD_UP);  //pull up to 3.3V,make GPIO1 a stable level
  while(1){
    int button = digitalRead(ButtonPin);
    if(button == 0 && last_button == 1){ //indicate that button has pressed down
        state = (state + 1)%3;   //led on
    }
    last_button = button;
    printf("State = %d", state);
       
  }

  return 0;
}

Button Operated Stopwatch

After we were able to move through the states successfully, we integrated time tracking into the program. We used the <time.h> library for that purpose, recording the time when we switch into state 1 (stopwatch starts) and then recording the time elapsed since then at every iteration. Since we are only interested in seconds and the loop iterates multiple times in 1 sec, we specified that it should only output the time when the second changes.

/* Stopwatch
 * Joce Kofke, Stacy Falaleeva, Jessie Wang
 * 
 * Runs a stopwatch on the Raspberry Pi
 * 
 * to run:
 * gcc -o stopwatchtest stopwatchtest.c -lwiringpi
 */

#include <wiringPi.h>
#include <stdio.h>
#include <time.h>

int buttonpin = 1;
int seconds = 0;
int minutes = 0;
int state = 0;
int clap;
int lastClap = 0;
int lastSec = 0, currSec = 0;
clock_t start, end;

void stop(){
  return;
}

void reset(){
  seconds = 0;
  minutes = 0;
}

void setup(){
  wiringPiSetup();
  pinMode(buttonpin, INPUT);
  pullUpDnControl( buttonpin, PUD_UP);
}


void loop(){

  clap = digitalRead(buttonpin);

  //printf("%d\n", digitalRead(buttonpin));
  //return;
  
  if (clap == HIGH && lastClap == LOW){
    state = (state + 1) % 3;
    if (state == 1){
      start = clock();   // started the stopwatch, record start time

    }
  }

  //stopwatch is running
  if (state == 1){
   end = clock();  // record what time it is now
    currSec = ((int)(end-start))/CLOCKS_PER_SEC;    //calculate time elapsed since start
                                                    //cast into int, so it round to nearest second
    // only print if rolls over into the next second
    if (lastSec != currSec){
       printf("%d\n", currSec);
    }
    lastSec = currSec;
  }

  else if (state == 2){
    stop();
  }

  else {
     printf("READY");
  }

  // printf("%d\n", state);
  // printf("%d:%d\n", minutes, seconds);

  lastClap = clap;
}

int main(int argc, char *argv[]){
  printf("hello\n");
  setup();
  printf("there\n");
  while(1){
    loop();
  }
  return 0;
}