Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int ledPin = 9;
- // button connected to digital pin 10
- const int buttonPin = 10;
- // Length of a dash in ms
- const int dash = 300;
- // length of a dot in ms
- const int dot = 100;
- // state of the button this iteration
- byte current_button = LOW;
- // previous state of the button
- byte old_button = LOW;
- // start of a pressed event
- unsigned long int start_high;
- // end of a pressed event
- unsigned long int stop_high;
- // start of a non-pressed event
- unsigned long int start_low;
- // end of a non-pressed event
- unsigned long int stop_low;
- //Check whether or not you're on the second digit of the
- //temperature
- boolean step2 = false;
- //record the sequence of dots and dashes for the first digit
- int morse_sequence1[5] = {
- 0,0,0,0,0};
- //record morse sequence for second digit;
- int morse_sequence2[5] = {
- 0,0,0,0,0};
- int temperature = 0;
- // The setup() method runs once,
- // when the sketch starts
- void setup(){
- // initialize the button pin as an input:
- pinMode(buttonPin, INPUT);
- pinMode(ledPin, OUTPUT);
- // Set up serial communication
- Serial.begin(9600);
- }
- void loop() {
- // Read the current state
- current_button = read_button(buttonPin, old_button);
- // LOW -> HIGH transition
- if ((old_button == LOW) && (current_button == HIGH)){
- // Update timing
- start_high = millis();
- stop_low = start_high;
- }
- // HIGH -> LOW transition
- if ((old_button == HIGH) && (current_button == LOW)){
- // Update timing
- stop_high = millis();
- start_low = stop_high;
- // detect dashs and dots
- if ((stop_high - start_high) >= dash){
- Serial.print("-");
- rec_sequence(2);
- }
- else{
- if ((stop_high - start_high) >= dot){
- Serial.print(".");
- rec_sequence(1);
- }
- }
- }
- old_button = current_button;
- }
- byte read_button(byte pin, byte ref_value) {
- // observed the state of the button
- byte current_button = digitalRead(pin);
- // There is a LOW -> HIGH transition
- // or a HIGH -> LOW transition
- if (((ref_value == LOW) && (current_button == HIGH))
- || ((ref_value == HIGH) && (current_button == LOW))){
- // wait for a while
- delay(5);
- // update the state of the button
- current_button = digitalRead(pin);
- }
- return(current_button);
- }
- /*record the sequence of button pushes, with 1 representing a dot
- and 2 representing a dash*/
- void rec_sequence(int code){
- if(morse_sequence1[4] == 0){
- int i = 0;
- while(morse_sequence1[i] != 0){
- i++;
- }
- morse_sequence1[i] = (code == 1) ? 1 : 2;
- if(i == 4){
- parse_sequence(morse_sequence1);
- }
- }
- else if(morse_sequence2[4] == 0){
- int i = 0;
- while(morse_sequence2[i] != 0){
- i++;
- }
- //if the code is 1 (a dot), record the sequence as a 1, or
- //as a 2
- morse_sequence2[i] = (code == 1) ? 1 : 2;
- if(i == 4){
- step2 = true;
- Serial.print(" ");
- parse_sequence(morse_sequence2);
- }
- }
- }
- void parse_sequence(int sequence[]){
- int one[5] = {1,2,2,2,2};
- int two[5] = {1,1,2,2,2};
- int three[5] = {1,1,1,2,2};
- int four[5] = {1,1,1,1,2};
- int five[5] = {1,1,1,1,1};
- int six[5] = {2,1,1,1,1};
- int seven[5] = {2,2,1,1,1};
- int eight[5] = { 2,2,2,1,1};
- int nine[5] = { 2,2,2,2,1};
- int zero[5] = {2,2,2,2,2};
- if(check_array(sequence, one)){
- temperature = (step2) ? temperature*10 + 1 : 1;
- }else if(check_array(sequence, two)){
- temperature = (step2) ? temperature*10 + 2 : 2;
- }else if(check_array(sequence, three)){
- temperature = (step2) ? temperature*10 + 3 : 3;
- }else if(check_array(sequence, four)){
- temperature = (step2) ? temperature*10 + 4 : 4;
- }else if(check_array(sequence, five)){
- temperature = (step2) ? temperature*10 + 5 : 5;
- }else if(check_array(sequence, six)){
- temperature = (step2) ? temperature*10 + 6 : 6;
- }else if(check_array(sequence, seven)){
- temperature = (step2) ? temperature*10 + 7 : 7;
- }else if(check_array(sequence, eight)){
- temperature = (step2) ? temperature*10 + 8 : 8;
- }else if(check_array(sequence, nine)){
- temperature = (step2) ? temperature*10 + 9 : 9;
- }else if(check_array(sequence, zero)){
- temperature = (step2) ? temperature*10 + 0 : 0;
- }
- if(step2){
- motor_check(temperature);
- }
- }
- //check the temperature and turn on the fan if the temp is at least
- //70
- void motor_check(int temp){
- Serial.println();
- Serial.println(temp);
- if(temp >= 70){
- digitalWrite(ledPin,HIGH);
- }
- else{
- digitalWrite(ledPin, LOW);
- }
- }
- //check the morse code input against a given sequence
- boolean check_array(int code[],int ref_sequence[]){
- for(int i = 0; i < 5; i++){
- if(code[i] != ref_sequence[i]){
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement