Components and supplies
Switch
SparkFun 7-Segment Serial Display - Red
Arduino Nano R3
Rotary Encoder with Push-Button
Morse Code Key
Peizo transducer
Tools and machines
Soldering iron (generic)
dremel
Hot glue gun (generic)
Project description
Code
Kitchen timer code
arduino
Handle 2 encoders, peizo beeper, and 4x 7 segment displays
1/* 2jjn timer 320161022 4*/ 5#include <Wire.h> // Include the Arduino SPI library 6bool showColon=0; 7 8/*start encoders*/ 9int val; 10int encoder0PinA = 2; 11int encoder0PinB = 3; 12int encoder0Pos = 0; 13int encoder0PinALast = LOW; 14int encoder1PinA = 7; 15int encoder1PinB = 8; 16int encoder1Pos = 0; 17int encoder1PinALast = LOW; 18int n = LOW; 19bool startup = true; 20bool doBeeper=false; 21int mins=0; 22int secs=0; 23/*end encoders*/ 24 25/*beep*/ 26int SPKR=9; 27/*end beep*/ 28 29/*toggle button*/ 30 31// constants won't change. They're used here to 32// set pin numbers: 33const int buttonPin = 5; // the number of the pushbutton pin 34const int ledPin = 13; // the number of the LED pin 35 36// Variables will change: 37int ledState = HIGH; // the current state of the output pin 38int buttonState; // the current reading from the input pin 39int lastButtonState = LOW; // the previous reading from the input pin 40 41// the following variables are long's because the time, measured in miliseconds, 42// will quickly become a bigger number than can be stored in an int. 43long lastDebounceTime = 0; // the last time the output pin was toggled 44long debounceDelay = 50; // the debounce time; increase if the output flickers 45/*end toggle button*/ 46 47 48// Here we'll define the I2C address of our S7S. By default it 49// should be 0x71. This can be changed, though. 50const byte s7sAddress = 0x71; 51 52unsigned int counter = 0; // This variable will count up to 65k 53char tempString[10]; // Will be used with sprintf to create strings 54int scan=1; 55bool doCountdown=true; 56 57 58void setup(){ 59 Serial.begin(9600); 60 Wire.begin(); // Initialize hardware I2C pins 61 62 /*start encoder pins*/ 63 pinMode (encoder0PinA,INPUT); 64 pinMode (encoder0PinB,INPUT); 65 pinMode (encoder1PinA,INPUT); 66 pinMode (encoder1PinB,INPUT); 67 /*end encoder pins*/ 68 69 70 // Clear the display, and then turn on all segments and decimals 71 clearDisplayI2C(); // Clears display, resets cursor 72 73 // Custom function to send four bytes via I2C 74 // The I2C.write function only allows sending of a single 75 // byte at a time. 76 //s7sSendStringI2C("TIMR"); 77 //setDecimalsI2C(0b111111); // Turn on all decimals, colon, apos 78 79 // Flash brightness values at the beginning 80 //setBrightnessI2C(0); // Lowest brightness 81 //delay(130); 82 setBrightnessI2C(255); // High brightness 83 // setBrightnessI2C(105); // med brightness 84 //delay(150); 85 //s7sSendStringI2C("TIMR"); 86 //delay(150); 87 // Clear the display before jumping into loop 88 //clearDisplayI2C(); 89 90 /*toggle button*/ 91 pinMode(buttonPin, INPUT_PULLUP); 92 pinMode(ledPin, OUTPUT); 93 //pinMode (SPKR,OUTPUT); 94 95 // set initial LED state 96 //digitalWrite(ledPin, ledState); 97 /*end toggle button*/ 98} 99 100 101void loop(){ 102 /*read encoders*/ 103 int delta=0; 104 n = digitalRead(encoder0PinA); 105 if ((encoder0PinALast == LOW) && (n == HIGH)) { 106 if (digitalRead(encoder0PinB) == LOW) { 107 encoder0Pos++; 108 delta=1; 109 } else { 110 encoder0Pos--; 111 delta=-1; 112 } 113 Serial.print ("B"); 114 Serial.print (encoder0Pos); 115 Serial.print ("/"); 116 } 117 if(encoder0PinALast != n) { 118 StopBeeperAndCountdown(); 119 mins+=delta;//=encoder0Pos; 120 } 121 encoder0PinALast = n; 122 123 n = digitalRead(encoder1PinA); 124 if ((encoder1PinALast == LOW) && (n == HIGH)) { 125 if (digitalRead(encoder1PinB) == LOW) { 126 encoder1Pos++; 127 delta = 5; 128 } else { 129 encoder1Pos--; 130 delta = -5; 131 } 132 Serial.print ("A"); 133 Serial.print (encoder1Pos); 134 Serial.print ("/"); 135 } 136 if(encoder1PinALast != n){ 137 StopBeeperAndCountdown(); 138 secs+=delta;//encoder1Pos*5; 139 } 140 encoder1PinALast = n; 141 142 if(mins>99)mins=0; //mins 143 if(mins<0)mins=99; 144 if(secs>59)secs=0; //secs 145 if(secs<0)secs=55; 146 147 /*read encoders end*/ 148 if(startup){ 149 startup = false; 150 encoder0Pos = 0; 151 encoder1Pos = 0; 152 mins = 0; 153 secs = 0; 154 } 155 156 // Magical sprintf creates a string for us to send to the s7s. 157 // The %4d option creates a 4-digit integer. 158 //sprintf(tempString, "%4d", counter); 159 //sprintf(tempString, "%s%2d%s%2d",(mins<10)?"":"0", mins,(secs<10)?"":"0",secs); 160 if(mins>9&&secs>9)sprintf(tempString, "%2d%2d", mins,secs); 161 if(mins<=9&&secs>9)sprintf(tempString, "0%d%2d", mins,secs); 162 if(mins>9&&secs<=9)sprintf(tempString, "%2d0%d", mins,secs); 163 if(mins<9&&secs<=9)sprintf(tempString, "%02d0%d", mins,secs); 164 165 if(scan%1000==0){ //only update every so many cycles 166 // This will output the tempString to the S7S 167 s7sSendStringI2C(tempString); 168 } 169 // Print the decimal at the proper spot 170 /* 171 setDecimalsI2C(0b00000100); // Sets digit 3 decimal on 172 */ 173 174 //delay(50); // This will make the display update at 10Hz.*/ 175 176 scan++; 177 if(scan > 60000){scan=0;} 178 179 if(doCountdown){ 180 if(scan % 5000==0 && !doBeeper){ 181 if(secs >= 0)secs--; 182 if(secs < 0){ 183 secs = 59; 184 mins--; 185 } 186 if(mins < 0){ 187 secs = 0; 188 mins = 0; 189 doCountdown = false; 190 doBeeper = true; 191 } 192 } 193 } 194 195 if(scan % 3000 == 0){ 196 showColon=!showColon; 197 // [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1) 198 if(doCountdown && showColon){//showColon 199 setDecimalsI2C(0b010000); // Turn on colon 200 }else{ 201 setDecimalsI2C(0b000000); // hide colon 202 } 203 if(doBeeper){ 204 alarm(); 205 } 206 } 207 208 CheckToggleButton(); 209} 210 211void CheckToggleButton(){ 212 // read the state of the switch into a local variable: 213 int reading = digitalRead(buttonPin); 214 215 // check to see if you just pressed the button 216 // (i.e. the input went from LOW to HIGH), and you've waited 217 // long enough since the last press to ignore any noise: 218 219 // If the switch changed, due to noise or pressing: 220 if (reading != lastButtonState) { 221 // reset the debouncing timer 222 lastDebounceTime = millis(); 223 } 224 225 if ((millis() - lastDebounceTime) > debounceDelay) { 226 // whatever the reading is at, it's been there for longer 227 // than the debounce delay, so take it as the actual current state: 228 229 // if the button state has changed: 230 if (reading != buttonState) { 231 snick(); 232 if(doCountdown&&doBeeper)StopBeeperAndCountdown(); 233 buttonState = reading; 234 235 // only toggle the LED if the new button state is HIGH 236 if (buttonState == HIGH) { 237 //ledState = !ledState; 238 239 240 doCountdown=!doCountdown; 241 } 242 } 243 } 244 245 // set the LED: 246 //digitalWrite(ledPin, ledState); 247 248 // save the reading. Next time through the loop, 249 // it'll be the lastButtonState: 250 lastButtonState = reading; 251} 252 253// This custom function works somewhat like a serial.print. 254// You can send it an array of chars (string) and it'll print 255// the first 4 characters in the array. 256void s7sSendStringI2C(String toSend){ 257 Wire.beginTransmission(s7sAddress); 258 for (int i=0; i<4; i++) 259 { 260 Wire.write(toSend[i]); 261 } 262 Wire.endTransmission(); 263} 264 265// Send the clear display command (0x76) 266// This will clear the display and reset the cursor 267void clearDisplayI2C(){ 268 Wire.beginTransmission(s7sAddress); 269 Wire.write(0x76); // Clear display command 270 Wire.endTransmission(); 271} 272 273// Set the displays brightness. Should receive byte with the value 274// to set the brightness to 275// dimmest------------->brightest 276// 0--------127--------255 277void setBrightnessI2C(byte value){ 278 Wire.beginTransmission(s7sAddress); 279 Wire.write(0x7A); // Set brightness command byte 280 Wire.write(value); // brightness data byte 281 Wire.endTransmission(); 282} 283 284// Turn on any, none, or all of the decimals. 285// The six lowest bits in the decimals parameter sets a decimal 286// (or colon, or apostrophe) on or off. A 1 indicates on, 0 off. 287// [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1) 288void setDecimalsI2C(byte decimals){ 289 Wire.beginTransmission(s7sAddress); 290 Wire.write(0x77); 291 Wire.write(decimals); 292 Wire.endTransmission(); 293} 294 295void snick(){ 296 // put your setup code here, to run once: 297 pinMode (SPKR,OUTPUT); 298 299 for (int i=0; i<300; i++) { // generate a 1KHz tone for 1/2 second 300 digitalWrite(SPKR, HIGH); 301 delayMicroseconds(100); 302 digitalWrite(SPKR, LOW); 303 delayMicroseconds(100); 304 } 305 pinMode (SPKR,INPUT); 306 //delay(1000); 307} 308 309void alarm(){ 310 //return; 311 // put your setup code here, to run once: 312 pinMode (SPKR,OUTPUT); 313 314 for (int i=0; i<350; i++) { 315 digitalWrite(SPKR, HIGH); 316 delayMicroseconds(120); 317 digitalWrite(SPKR, LOW); 318 delayMicroseconds(50); 319 digitalWrite(SPKR, HIGH); 320 delayMicroseconds(60); 321 } 322 pinMode (SPKR,INPUT); 323 //delay(1000); 324} 325 326void StopBeeperAndCountdown(){ 327 doBeeper=false; 328 doCountdown=false; 329 //secs=30; 330 //mins=encoder1Pos*5; 331 //secs=encoder0Pos*5; 332} 333
Kitchen timer code
arduino
Handle 2 encoders, peizo beeper, and 4x 7 segment displays
1/* 2jjn timer 320161022 4*/ 5#include <Wire.h> // Include the Arduino SPI library 6bool showColon=0; 7 8/*start encoders*/ 9int val; 10int encoder0PinA = 2; 11int encoder0PinB = 3; 12int encoder0Pos = 0; 13int encoder0PinALast = LOW; 14int encoder1PinA = 7; 15int encoder1PinB = 8; 16int encoder1Pos = 0; 17int encoder1PinALast = LOW; 18int n = LOW; 19bool startup = true; 20bool doBeeper=false; 21int mins=0; 22int secs=0; 23/*end encoders*/ 24 25/*beep*/ 26int SPKR=9; 27/*end beep*/ 28 29/*toggle button*/ 30 31// constants won't change. They're used here to 32// set pin numbers: 33const int buttonPin = 5; // the number of the pushbutton pin 34const int ledPin = 13; // the number of the LED pin 35 36// Variables will change: 37int ledState = HIGH; // the current state of the output pin 38int buttonState; // the current reading from the input pin 39int lastButtonState = LOW; // the previous reading from the input pin 40 41// the following variables are long's because the time, measured in miliseconds, 42// will quickly become a bigger number than can be stored in an int. 43long lastDebounceTime = 0; // the last time the output pin was toggled 44long debounceDelay = 50; // the debounce time; increase if the output flickers 45/*end toggle button*/ 46 47 48// Here we'll define the I2C address of our S7S. By default it 49// should be 0x71. This can be changed, though. 50const byte s7sAddress = 0x71; 51 52unsigned int counter = 0; // This variable will count up to 65k 53char tempString[10]; // Will be used with sprintf to create strings 54int scan=1; 55bool doCountdown=true; 56 57 58void setup(){ 59 Serial.begin(9600); 60 Wire.begin(); // Initialize hardware I2C pins 61 62 /*start encoder pins*/ 63 pinMode (encoder0PinA,INPUT); 64 pinMode (encoder0PinB,INPUT); 65 pinMode (encoder1PinA,INPUT); 66 pinMode (encoder1PinB,INPUT); 67 /*end encoder pins*/ 68 69 70 // Clear the display, and then turn on all segments and decimals 71 clearDisplayI2C(); // Clears display, resets cursor 72 73 // Custom function to send four bytes via I2C 74 // The I2C.write function only allows sending of a single 75 // byte at a time. 76 //s7sSendStringI2C("TIMR"); 77 //setDecimalsI2C(0b111111); // Turn on all decimals, colon, apos 78 79 // Flash brightness values at the beginning 80 //setBrightnessI2C(0); // Lowest brightness 81 //delay(130); 82 setBrightnessI2C(255); // High brightness 83 // setBrightnessI2C(105); // med brightness 84 //delay(150); 85 //s7sSendStringI2C("TIMR"); 86 //delay(150); 87 // Clear the display before jumping into loop 88 //clearDisplayI2C(); 89 90 /*toggle button*/ 91 pinMode(buttonPin, INPUT_PULLUP); 92 pinMode(ledPin, OUTPUT); 93 //pinMode (SPKR,OUTPUT); 94 95 // set initial LED state 96 //digitalWrite(ledPin, ledState); 97 /*end toggle button*/ 98} 99 100 101void loop(){ 102 /*read encoders*/ 103 int delta=0; 104 n = digitalRead(encoder0PinA); 105 if ((encoder0PinALast == LOW) && (n == HIGH)) { 106 if (digitalRead(encoder0PinB) == LOW) { 107 encoder0Pos++; 108 delta=1; 109 } else { 110 encoder0Pos--; 111 delta=-1; 112 } 113 Serial.print ("B"); 114 Serial.print (encoder0Pos); 115 Serial.print ("/"); 116 } 117 if(encoder0PinALast != n) { 118 StopBeeperAndCountdown(); 119 mins+=delta;//=encoder0Pos; 120 } 121 encoder0PinALast = n; 122 123 n = digitalRead(encoder1PinA); 124 if ((encoder1PinALast == LOW) && (n == HIGH)) { 125 if (digitalRead(encoder1PinB) == LOW) { 126 encoder1Pos++; 127 delta = 5; 128 } else { 129 encoder1Pos--; 130 delta = -5; 131 } 132 Serial.print ("A"); 133 Serial.print (encoder1Pos); 134 Serial.print ("/"); 135 } 136 if(encoder1PinALast != n){ 137 StopBeeperAndCountdown(); 138 secs+=delta;//encoder1Pos*5; 139 } 140 encoder1PinALast = n; 141 142 if(mins>99)mins=0; //mins 143 if(mins<0)mins=99; 144 if(secs>59)secs=0; //secs 145 if(secs<0)secs=55; 146 147 /*read encoders end*/ 148 if(startup){ 149 startup = false; 150 encoder0Pos = 0; 151 encoder1Pos = 0; 152 mins = 0; 153 secs = 0; 154 } 155 156 // Magical sprintf creates a string for us to send to the s7s. 157 // The %4d option creates a 4-digit integer. 158 //sprintf(tempString, "%4d", counter); 159 //sprintf(tempString, "%s%2d%s%2d",(mins<10)?"":"0", mins,(secs<10)?"":"0",secs); 160 if(mins>9&&secs>9)sprintf(tempString, "%2d%2d", mins,secs); 161 if(mins<=9&&secs>9)sprintf(tempString, "0%d%2d", mins,secs); 162 if(mins>9&&secs<=9)sprintf(tempString, "%2d0%d", mins,secs); 163 if(mins<9&&secs<=9)sprintf(tempString, "%02d0%d", mins,secs); 164 165 if(scan%1000==0){ //only update every so many cycles 166 // This will output the tempString to the S7S 167 s7sSendStringI2C(tempString); 168 } 169 // Print the decimal at the proper spot 170 /* 171 setDecimalsI2C(0b00000100); // Sets digit 3 decimal on 172 */ 173 174 //delay(50); // This will make the display update at 10Hz.*/ 175 176 scan++; 177 if(scan > 60000){scan=0;} 178 179 if(doCountdown){ 180 if(scan % 5000==0 && !doBeeper){ 181 if(secs >= 0)secs--; 182 if(secs < 0){ 183 secs = 59; 184 mins--; 185 } 186 if(mins < 0){ 187 secs = 0; 188 mins = 0; 189 doCountdown = false; 190 doBeeper = true; 191 } 192 } 193 } 194 195 if(scan % 3000 == 0){ 196 showColon=!showColon; 197 // [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1) 198 if(doCountdown && showColon){//showColon 199 setDecimalsI2C(0b010000); // Turn on colon 200 }else{ 201 setDecimalsI2C(0b000000); // hide colon 202 } 203 if(doBeeper){ 204 alarm(); 205 } 206 } 207 208 CheckToggleButton(); 209} 210 211void CheckToggleButton(){ 212 // read the state of the switch into a local variable: 213 int reading = digitalRead(buttonPin); 214 215 // check to see if you just pressed the button 216 // (i.e. the input went from LOW to HIGH), and you've waited 217 // long enough since the last press to ignore any noise: 218 219 // If the switch changed, due to noise or pressing: 220 if (reading != lastButtonState) { 221 // reset the debouncing timer 222 lastDebounceTime = millis(); 223 } 224 225 if ((millis() - lastDebounceTime) > debounceDelay) { 226 // whatever the reading is at, it's been there for longer 227 // than the debounce delay, so take it as the actual current state: 228 229 // if the button state has changed: 230 if (reading != buttonState) { 231 snick(); 232 if(doCountdown&&doBeeper)StopBeeperAndCountdown(); 233 buttonState = reading; 234 235 // only toggle the LED if the new button state is HIGH 236 if (buttonState == HIGH) { 237 //ledState = !ledState; 238 239 240 doCountdown=!doCountdown; 241 } 242 } 243 } 244 245 // set the LED: 246 //digitalWrite(ledPin, ledState); 247 248 // save the reading. Next time through the loop, 249 // it'll be the lastButtonState: 250 lastButtonState = reading; 251} 252 253// This custom function works somewhat like a serial.print. 254// You can send it an array of chars (string) and it'll print 255// the first 4 characters in the array. 256void s7sSendStringI2C(String toSend){ 257 Wire.beginTransmission(s7sAddress); 258 for (int i=0; i<4; i++) 259 { 260 Wire.write(toSend[i]); 261 } 262 Wire.endTransmission(); 263} 264 265// Send the clear display command (0x76) 266// This will clear the display and reset the cursor 267void clearDisplayI2C(){ 268 Wire.beginTransmission(s7sAddress); 269 Wire.write(0x76); // Clear display command 270 Wire.endTransmission(); 271} 272 273// Set the displays brightness. Should receive byte with the value 274// to set the brightness to 275// dimmest------------->brightest 276// 0--------127--------255 277void setBrightnessI2C(byte value){ 278 Wire.beginTransmission(s7sAddress); 279 Wire.write(0x7A); // Set brightness command byte 280 Wire.write(value); // brightness data byte 281 Wire.endTransmission(); 282} 283 284// Turn on any, none, or all of the decimals. 285// The six lowest bits in the decimals parameter sets a decimal 286// (or colon, or apostrophe) on or off. A 1 indicates on, 0 off. 287// [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1) 288void setDecimalsI2C(byte decimals){ 289 Wire.beginTransmission(s7sAddress); 290 Wire.write(0x77); 291 Wire.write(decimals); 292 Wire.endTransmission(); 293} 294 295void snick(){ 296 // put your setup code here, to run once: 297 pinMode (SPKR,OUTPUT); 298 299 for (int i=0; i<300; i++) { // generate a 1KHz tone for 1/2 second 300 digitalWrite(SPKR, HIGH); 301 delayMicroseconds(100); 302 digitalWrite(SPKR, LOW); 303 delayMicroseconds(100); 304 } 305 pinMode (SPKR,INPUT); 306 //delay(1000); 307} 308 309void alarm(){ 310 //return; 311 // put your setup code here, to run once: 312 pinMode (SPKR,OUTPUT); 313 314 for (int i=0; i<350; i++) { 315 digitalWrite(SPKR, HIGH); 316 delayMicroseconds(120); 317 digitalWrite(SPKR, LOW); 318 delayMicroseconds(50); 319 digitalWrite(SPKR, HIGH); 320 delayMicroseconds(60); 321 } 322 pinMode (SPKR,INPUT); 323 //delay(1000); 324} 325 326void StopBeeperAndCountdown(){ 327 doBeeper=false; 328 doCountdown=false; 329 //secs=30; 330 //mins=encoder1Pos*5; 331 //secs=encoder0Pos*5; 332} 333
Downloadable files
Breadboard image
Breadboard image
Schematic
Here is the kitchen timer breadboard
Schematic
Schematic
Here is the kitchen timer breadboard
Schematic
Vero board version
Vero board version
Breadboard image
Breadboard image
Comments
Only logged in users can leave comments
oklima
0 Followers
•0 Projects
Table of contents
Intro
3
0