Created
December 16, 2016 16:13
-
-
Save Miceuz/ec4176ce19f1eaaa87130e2bad5a79f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<SPI.h> | |
#include <Tone.h> | |
Tone player; | |
void beep(){ | |
player.play(4000); | |
delay(100); | |
player.stop(); | |
} | |
void setup() { | |
pinMode(10, OUTPUT); | |
pinMode(9, OUTPUT); | |
pinMode(8, OUTPUT); | |
digitalWrite(10, HIGH); | |
SPI.begin(); | |
SPI.setDataMode(SPI_MODE0); | |
Serial.begin(9600); | |
pinMode(7, INPUT); | |
pinMode(6, OUTPUT); | |
digitalWrite(6, LOW); | |
player.begin(3); | |
beep(); | |
} | |
#define ZERO 0b01000010 | |
#define ONE 0b11101110 | |
#define TWO 0b00110010 | |
#define THREE 0b00100110 | |
#define FOUR 0b10001110 | |
#define FIVE 0b00000111 | |
#define SIX 0b00000011 | |
#define SEVEN 0b01101110 | |
#define EIGHT 0b00000010 | |
#define NINE 0b00000110 | |
uint8_t digits[] = {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE}; | |
void digit(uint8_t d, uint8_t b) { | |
if(0 == d) { | |
digitalWrite(9, LOW); | |
digitalWrite(8, HIGH); | |
} else { | |
digitalWrite(8, LOW); | |
digitalWrite(9, HIGH); | |
} | |
digitalWrite(10, LOW); | |
SPI.transfer(digits[b]); | |
digitalWrite(10, HIGH); | |
} | |
uint8_t i = 1; | |
uint8_t d1 = 0; | |
uint8_t d2 = 0; | |
uint32_t lastDigitFlip = 0; | |
uint8_t d = 0; | |
void loop2() { | |
digit(0, i); | |
i = i << 1; | |
if(i == 0) i = 1; | |
delay(1000); | |
} | |
void delayZerocrossings(uint8_t zc) { | |
for(int i = 0; i < zc; i++) { | |
delay(10); | |
} | |
} | |
void pulse(uint8_t times) { | |
player.play(4000); | |
digitalWrite(6, HIGH); | |
delayZerocrossings(times); | |
digitalWrite(6, LOW); | |
player.stop(); | |
} | |
void outputPulseTrain(uint8_t length, uint8_t times) { | |
for(uint8_t i = 0; i < times; i++) { | |
pulse(length); | |
delayZerocrossings(length); | |
} | |
} | |
void loop() { | |
if(millis() - lastDigitFlip > 10) { | |
if(0 == d) { | |
digit(0, map(analogRead(A0), 0, 1010, 1, 9)); | |
d = 1; | |
} else { | |
digit(1, map(analogRead(A1), 0, 1010, 1, 9)); | |
d = 0; | |
} | |
lastDigitFlip = millis(); | |
} | |
if(LOW == digitalRead(7)) { | |
uint8_t length = map(analogRead(A1), 0, 1010, 1, 9); | |
uint8_t times = map(analogRead(A0), 0, 1010, 1, 9); | |
outputPulseTrain(length*5, times); | |
delay(500); | |
while(LOW == digitalRead(7)){ | |
//nothing | |
} | |
delay(500); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment