Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Triggering Stop Watch

Status
Not open for further replies.
The SN74123 should work fine. I have used them in One Shot applications in the past.
 
Hi Everyone,
I thought I would try to attempt this with the Arduino with a minimal parts count & have modified some sketches to give it a go.
It seems to be working perfectly but can someone please have a look at the code & see where I may be able to improve on it or correct it as I fumble with this sort of thing.
I guess it is all over the place, this may be embarrassing?
Code attached below:

Also, I have the Arduino Triggering it's own interrupts back into itself so to speak to trigger the stop watch, should I use a 1k resistor or so in series doing it this way?
Or is this totally wrong to do?
Cheers
Code:
/*
To Trigger Stop Watch ON & OFF At Pre Set Rising Voltages.
willeng
11/1/2015
Modified Sketches
Stop Watch To Trigger ON at ref1 Voltage & OFF at ref2 Voltage.
ref1 Turns Stop Watch ON
ref2 Turns Stop Watch OFF
  Arduino Mega 2560
  MAX7219  7 Segment Module  
  VCC  --------------->  5.0V
  DIN  --------------->  Pin 7
  CLK  --------------->  Pin 6
  LOAD  --------------->  Pin 5
  GND  --------------->  GND
  ////////////////////////////////////////////////////////////////////////////////////////////
  Trigger Connections:
  Output From LM2917N (0-5V max) Connected To (Pins A0 & A1)
  Digital Output - 'ref1' (pin 8)
  Digital Output - 'ref2' (pin 9)
  ////////////////////////////////////////////////////////////////////////////////////////////
  
  Interrupt 0 Pin 2 Mega 2560
  Interrupt 5 Pin 18 Mega 2560  
*/

#include "LedControl.h"
#include <plcLib.h>

LedControl lc=LedControl(7,6,5,1);

unsigned int START = 1;  // Trigger Start-up state (START = 1 to automatically start here)
int fsec, sec, minute, i, Start;
long previousMillis = 0;
long interval = 100;

//  Trigger
unsigned int ref1 = 300;  // Example:
//  (ref1) lower threshold = 307
//  (30% of 1024 = 1024 * 0.3 = 307)
//  (ref1) Lower threshold voltage = Vsupply * 0.3
//  (1.5 V if Vsupply = 5 V)
  
unsigned int ref2 = 600;  // (ref2 upper threshold = 717
//  (70% of 1024 = 1024 * 0.7 = 717)
//  Upper threshold voltage = Vsupply * 0.7
//  (3.5 V if Vsupply = 5 V)
//////////////////////////////////////////////////////////////////////////////
void setup()
{
  //  Trigger
  setupPLC();  // Setup inputs and outputs
  ///////////////////////////////////////////////////////////////

  // the zero refers to the MAX7219 number, it is zero for 1 chip
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,2);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
  fsec = 0;
  sec = 0;
  minute = 0;
  Start = 0;
  Disp_Data();

  attachInterrupt(0, Trigger, RISING);
  attachInterrupt(5, Trigger, RISING);
}
void loop()
{
  //  Trigger
  in(START);  // Read Start-up state
  inAnalog(A0);  // Read Analogue Input A0
  compareGT(ref1);  // Is Pin A0 > lower threshold?
  out(8);  // Pin 8 = 1 if Pin A0 > 300, Pin 8 = 0 otherwise
  reset(ref1);  // Cancel (ref1)

  inAnalog(A1);  // Read Analogue Input A1
  compareGT(ref2);  // Is Pin A1 > upper threshold?
  out(9);  // Pin 9 = 1 if Pin A1 > 600, Pin 9 = 0 otherwise
  reset(ref2);  // Cancel (ref2)

  //  Max 7219
  if(Start == 1) {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
  previousMillis = currentMillis;
  fsec = fsec+10;
  if(fsec == 100) {
  fsec = 0;
  sec = sec+1;
  if (sec == 100) {
  sec = 0;
  minute = minute+1;
  if(minute == 100) minute = 0;
  }
  }
  Disp_Data();
  } 
  }
}

void Trigger(){
if (Start == 1) {
  Start = 0;
}
else if (Start == 0){
  Start = 1;
}
}

void Disp_Data(){
  int ones, tens;
  ones = fsec%10;
  tens = (fsec/10)%10;
  lc.setDigit(0,0,(byte)tens,false);
  ones = sec%10;
  tens = (sec/10)%10;
  lc.setChar(0, 1,'-',false);
  lc.setDigit(0,2,(byte)ones,false);
  lc.setDigit(0,3,(byte)tens,false);

}

[code]
 
Last edited:
It doesn't look horrendously messy, so i would not worry about it.

Its not 30% of 1024,but 30% of 1023.

Think about it, 0 to 100 and 30% is 30. The max count, not the max count-1.
 
Hi Kiss,
Haha, it doesn't look horrendously messy.
There is a BIG gap between tidy & horrendously messy, very wide indeed:)

It works so I guess that's a start, just thought I would give it a go.
Thanks for pointing out the 1023 & not 1024.

Cheers
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top