Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
/*
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]