Hello
I am making a stop watch with a 4 segment display and RBG LED's as well as a buzzer that uses arcade sanwa style buttons. So far I've made good progress. I don't have my seven segment display yet so I am trying to use the serial monitor to keep track of the time.
The basic premise is that the green button starts and stops the time and the red button resets. The lights go green while the stopwatch is counting and go red when you reset. It features a buzzer that makes a tone when you start stop and reset.
I cant seem to find any good examples that use the Arduino time functions. If any one could point me to some good examples that I can use to get the serial.print function to report the time would be great. Here is my fritzing of the project so far. I will be using when it arrives a seven segment display that only uses 4 wires. So the wiring may change.
and I know it has no timer in it but here is the code so far
#include <TM1637.h>
#include <FastLED.h>
#include <JC_Button.h>
#define NOTE_D5 587
#define NOTE_G5 784
#define PIN_STRIP1 7
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 4
uint8_t max_bright = 255;
const int buzzer = 12;
CRGB leds[NUM_LEDS];
const int
PIN_BUTTON1 (8),
PIN_RESET (9);
Button BUTTON1(PIN_BUTTON1);
Button BUTTON2(PIN_RESET);
int NumStrip1 = 0;
void setup() {
// put your setup code here, to run once:
FastLED.setBrightness(max_bright);
FastLED.clear();
FastLED.show();
BUTTON1.begin();
BUTTON2.begin();
FastLED.addLeds<NEOPIXEL, PIN_STRIP1>(leds, NUM_LEDS); //initalize the first strand
leds[0] = CRGB::Red; //set all RGB's Starting Colors
FastLED.show();
leds[1] = CRGB::Red;
FastLED.show();
leds[2] = CRGB::Red;
FastLED.show();
leds[3] = CRGB::Red;
FastLED.show();
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
}
void loop() {
Checkbuttons();
}
void Checkbuttons()
{
BUTTON1.read(); // read BUTTON1
if (BUTTON1.wasReleased()) ChangeStrip1 ();
BUTTON2.read(); // read BUTTON2
if (BUTTON2.wasReleased()) ResetStrips ();
}
void ChangeStrip1 () {
leds[0] = CRGB::Green; //set all RGB's Starting Colors
FastLED.show();
leds[1] = CRGB::Green;
FastLED.show();
leds[2] = CRGB::Green;
FastLED.show();
leds[3] = CRGB::Green;
FastLED.show();
Serial.println("stopwatch");
buzz();
}
void ResetStrips () {
leds[0] = CRGB::Red; //set all RGB's Starting Colors
FastLED.show();
leds[1] = CRGB::Red;
FastLED.show();
leds[2] = CRGB::Red;
FastLED.show();
leds[3] = CRGB::Red;
FastLED.show();
buzz2();
}
void buzz() {
tone(buzzer, NOTE_D5);
delay(500);
noTone(buzzer);
}
void buzz2() {
tone(buzzer, NOTE_G5);
delay(500);
noTone(buzzer);
}
any help would be much appreciated. Just a note that I am using ws2812b RBG's so they only use 3 wires and are daisy chained together and work just fine the buzzer is working 100% as well and so do the buttons.
My 4 digit display has arrived and it use's the TM1637 IC and has only four wires a 5v a GND and CLK and DIO. I got it running with some example code and it counts perfect from 1 second to 99 minutes and 59 seconds. The only problem is I have no clue on how to control this with buttons. I also would like to have the LED's change depending on what the clock is doing to give a visual cue to a quick glance. all green its counting red and its been reset and stopped and Orange its just stopped. Here is my code so far.
//#include <TM1637.h>
#include <FastLED.h>
//#include <time.h>
//#include <TimeLib.h>
#include <JC_Button.h>
#define NOTE_D5 587 //music notes for buzzer
#define NOTE_G5 784
#define PIN_STRIP1 7
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 4
#include <TimerOne.h>
#include "TM1637.h"
#define ON 1
#define OFF 0
int8_t TimeDisp[] = {0x00,0x00,0x00,0x00};
unsigned char ClockPoint = 1;
unsigned char Update;
unsigned char halfsecond = 0;
unsigned char second = 0;
unsigned char minute = 0;
unsigned char hour = 12;
#define CLK 3//pins definitions for TM1637 and can be changed to other ports
#define DIO 2
TM1637 tm1637(CLK,DIO);
uint8_t max_bright = 255;
const int buzzer = 12;
CRGB leds[NUM_LEDS];
const int
PIN_BUTTON1 (8),
PIN_RESET (9);
Button BUTTON1(PIN_BUTTON1);
Button BUTTON2(PIN_RESET);
int NumStrip1 = 0;
void setup() {
// put your setup code here, to run once:
FastLED.setBrightness(max_bright);//LED CODE
FastLED.clear();
FastLED.show();
BUTTON1.begin();//Button initalize
BUTTON2.begin();
FastLED.addLeds<NEOPIXEL, PIN_STRIP1>(leds, NUM_LEDS); //initalize the first strand
leds[0] = CRGB::Red; //set all RGB's Starting Colors
FastLED.show();
leds[1] = CRGB::Red;
FastLED.show();
leds[2] = CRGB::Red;
FastLED.show();
leds[3] = CRGB::Red;
FastLED.show();
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
tm1637.set();
tm1637.init();
Timer1.initialize(500000);//timing for 500ms
Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR
}
void loop() {
if(Update == ON) //clock counter from seconds to minutes on the 4 digits
{ // example code need to control it with buttons
TimeUpdate();
tm1637.display(TimeDisp);
}
}
void Checkbuttons()
{
if(Update == ON) //a weak attempt to control the counter with buttons
{
TimeUpdate();
tm1637.display(TimeDisp);
}
//BUTTON1.read(); // read BUTTON1
// if (BUTTON1.wasReleased()) ChangeStrip1 ();
// BUTTON2.read(); // read BUTTON2
// if (BUTTON2.wasReleased()) ResetStrips ();
}
void ChangeStrip1 () {
leds[0] = CRGB::Green; //set all RGB's Starting Colors
FastLED.show();
leds[1] = CRGB::Green; //a function to change the LED Colors need to call when button is pressed.
FastLED.show();
leds[2] = CRGB::Green;
FastLED.show();
leds[3] = CRGB::Green;
FastLED.show();
Serial.println("stopwatch");
buzz();
}
void ResetStrips () { //a reset function to change the lights red when reset is pushed.
leds[0] = CRGB::Red; //set all RGB's Starting Colors
FastLED.show();
leds[1] = CRGB::Red;
FastLED.show();
leds[2] = CRGB::Red;
FastLED.show();
leds[3] = CRGB::Red;
FastLED.show();
buzz2();
}
void buzz() { //buzzer code
tone(buzzer, NOTE_D5);
delay(500);
noTone(buzzer);
}
void TimingISR() //part of the timer example for use with the 4 digit display
{
halfsecond ++;
Update = ON;
if(halfsecond == 2){
second ++;
if(second == 60)
{
minute ++;
if(minute == 60)
{
hour ++;
if(hour == 24)hour = 0;
minute = 0;
}
second = 0;
}
halfsecond = 0;
}
// Serial.println(second);
ClockPoint = (~ClockPoint) & 0x01;
}
void TimeUpdate(void)
{
if(ClockPoint)tm1637.point(POINT_ON);
else tm1637.point(POINT_OFF);
TimeDisp[0] = minute / 10;
TimeDisp[1] = minute % 10;
TimeDisp[2] = second / 10;
TimeDisp[3] = second % 10;
Update = OFF;
}
void buzz2() {
tone(buzzer, NOTE_G5);
delay(500);
noTone(buzzer);
}
This display is real nice and I will post a pic of it soon but Banggood sells them for a great price groove sells them too. Real nice to work with only 4 wires. If anyone knows how to get buttons to control the action any assistance would be much appreciated. I am going to hopefully upon completion make this into a little hardboard box with the screen and buttons on top and the led's going round on the side. I know it will be a big stopwatch but the display is tres bon and I just love arcade buttons. So I think it will be more fun to use than my current stopwatch. Not as compact but hey what arduino project is compact right.
Too tired to go into details now but with a different approach to the code you can make each button and the buzzer into no-delay functions inside of loop() that ALWAYS watch the buttons and sound the buzzer as a triggered event that times itself out.
These would be cooperative tasks. Another would read button state variables that the button watch tasks update and logically decide what to do based on what has happened, the process state. When the buzzer should buzz, a variable that the buzzer task checks is set to trigger the buzzer task --and it runs itself until done when it clears the flag.
Do you see how pulling these things apart means not having to structure them into one big mono-block where control is layers deep iffy-theny?
It is easier to write and debug small tasks in side-sketches then import to the main sketch to co-ordinate?
The lessons to do this are not experts or advanced programmers only material. What they are is a different way than what most all of us first got. They don't need commands beyond what beginners need to learn, they don't need pointers or bitmath to work but you should know how to use switch-case or at least if-elseif-else logic.
The 1st 2 links in my signature space below are to tutorials that can get you started. Alternate to that there is the
Demonstration code for several things at the same time thread near the top of the Project Guidance section of the forum where this thread is posted. The Arduino Built-In (to your IDE) Example named Blink Without Delay that actually has some errors but they won't matter for almost 25 days, still bad lesson to teach mixing signed and unsigned variables when time-keeping.
moJoeRedRog:
Not as compact but hey what arduino project is compact right.
Just by the way, there are boucoup belle Arduino projects shown on the web. The USB business card comes to mind and the many many data sense and log projects that fit in Altoid tins including batteries to run month or longer.
The best tiny ones use tiny surface mount chips soldered to custom PCB maybe in a toaster over or fry pan by very accomplished hobbyists.
So hey, you got the day when you can do it as well to look forward to?
thank you for the guidance GoForSmoke, I am trying to re work my code using a switch case structure and if I still cant get things moving try a if else if style. I am only an enthusiast and my programming is weak but I can use your tips to get this project moving. Cheers!