Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1.  
  2. const int ledPin = 9;
  3. // button connected to digital pin 10
  4. const int buttonPin = 10;
  5. // Length of a dash in ms
  6. const int dash = 300;
  7. // length of a dot in ms
  8. const int dot = 100;
  9. // state of the button this iteration
  10. byte current_button = LOW;
  11. // previous state of the button
  12. byte old_button = LOW;
  13. // start of a pressed event  
  14. unsigned long int start_high;
  15. // end of a pressed event  
  16. unsigned long int stop_high;
  17. // start of a non-pressed event
  18. unsigned long int start_low;
  19. // end of a non-pressed event
  20. unsigned long int stop_low;
  21.  
  22. //Check whether or not you're on the second digit of the
  23. //temperature
  24. boolean step2 = false;
  25. //record the sequence of dots and dashes for the first digit
  26. int morse_sequence1[5] = {
  27.   0,0,0,0,0};
  28. //record morse sequence for second digit;
  29. int morse_sequence2[5] = {
  30.   0,0,0,0,0};
  31.  
  32. int temperature = 0;
  33.  
  34. // The setup() method runs once,
  35. // when the sketch starts
  36. void setup(){
  37.   // initialize the button pin as an input:
  38.   pinMode(buttonPin, INPUT);    
  39.   pinMode(ledPin, OUTPUT);
  40.   // Set up serial communication
  41.   Serial.begin(9600);  
  42. }
  43.  
  44. void loop() {
  45.   // Read the current state
  46.   current_button = read_button(buttonPin, old_button);
  47.  
  48.   // LOW -> HIGH transition
  49.   if ((old_button == LOW) && (current_button == HIGH)){    
  50.     // Update timing
  51.     start_high = millis();
  52.     stop_low = start_high;
  53.   }  
  54.   // HIGH -> LOW transition
  55.   if ((old_button == HIGH) && (current_button == LOW)){    
  56.     // Update timing
  57.     stop_high = millis();
  58.     start_low = stop_high;
  59.  
  60.     // detect dashs and dots      
  61.     if ((stop_high - start_high) >= dash){      
  62.       Serial.print("-");
  63.       rec_sequence(2);
  64.     }      
  65.     else{      
  66.       if ((stop_high - start_high) >= dot){        
  67.         Serial.print(".");
  68.         rec_sequence(1);
  69.       }        
  70.     }    
  71.   }    
  72.   old_button = current_button;
  73. }
  74.  
  75.  
  76. byte read_button(byte pin, byte ref_value) {
  77.   // observed the state of the button  
  78.   byte current_button = digitalRead(pin);
  79.   // There is a LOW -> HIGH transition  
  80.   // or a HIGH -> LOW transition
  81.   if (((ref_value == LOW) && (current_button == HIGH))
  82.     || ((ref_value == HIGH) && (current_button == LOW))){
  83.     // wait for a while
  84.     delay(5);
  85.     // update the state of the button
  86.     current_button = digitalRead(pin);
  87.   }  
  88.   return(current_button);
  89. }
  90.  
  91. /*record the sequence of button pushes, with 1 representing a dot
  92.  and 2 representing a dash*/
  93. void rec_sequence(int code){
  94.   if(morse_sequence1[4] == 0){
  95.     int i = 0;
  96.     while(morse_sequence1[i] != 0){
  97.       i++;
  98.     }
  99.  
  100.     morse_sequence1[i] = (code == 1) ? 1 : 2;
  101.  
  102.     if(i == 4){
  103.       parse_sequence(morse_sequence1);
  104.     }
  105.   }
  106.   else if(morse_sequence2[4] == 0){
  107.     int i = 0;
  108.  
  109.     while(morse_sequence2[i] != 0){
  110.       i++;
  111.     }
  112.     //if the code is 1 (a dot), record the sequence as a 1, or
  113.     //as a 2
  114.     morse_sequence2[i] = (code == 1) ? 1 : 2;
  115.     if(i == 4){
  116.       step2 = true;
  117.       Serial.print("  ");
  118.       parse_sequence(morse_sequence2);
  119.     }
  120.  
  121.   }
  122. }
  123.  
  124. void parse_sequence(int sequence[]){
  125.   int one[5] = {1,2,2,2,2};
  126.   int two[5] = {1,1,2,2,2};
  127.   int three[5] = {1,1,1,2,2};
  128.   int four[5] = {1,1,1,1,2};
  129.   int five[5] = {1,1,1,1,1};
  130.   int six[5] = {2,1,1,1,1};
  131.   int seven[5] = {2,2,1,1,1};
  132.   int eight[5] = { 2,2,2,1,1};
  133.   int nine[5] = { 2,2,2,2,1};
  134.   int zero[5] = {2,2,2,2,2};
  135.  
  136.  
  137.  
  138.  
  139.   if(check_array(sequence, one)){
  140.     temperature = (step2) ? temperature*10 + 1 : 1;
  141.    
  142.   }else if(check_array(sequence, two)){
  143.     temperature = (step2) ? temperature*10 + 2 : 2;
  144.    
  145.   }else if(check_array(sequence, three)){
  146.     temperature = (step2) ? temperature*10 + 3 : 3;
  147.    
  148.   }else if(check_array(sequence, four)){
  149.     temperature = (step2) ? temperature*10 + 4 : 4;
  150.    
  151.   }else if(check_array(sequence, five)){
  152.     temperature = (step2) ? temperature*10 + 5 : 5;
  153.    
  154.   }else if(check_array(sequence, six)){
  155.     temperature = (step2) ? temperature*10 + 6 : 6;
  156.  
  157.   }else if(check_array(sequence, seven)){
  158.     temperature = (step2) ? temperature*10 + 7 : 7;
  159.  
  160.   }else if(check_array(sequence, eight)){
  161.     temperature = (step2) ? temperature*10 + 8 : 8;
  162.  
  163.   }else if(check_array(sequence, nine)){
  164.     temperature = (step2) ? temperature*10 + 9 : 9;
  165.    
  166.   }else if(check_array(sequence, zero)){
  167.     temperature = (step2) ? temperature*10 + 0 : 0;
  168.   }
  169.  
  170.   if(step2){
  171.     motor_check(temperature);
  172.   }
  173. }
  174.  
  175.  
  176. //check the temperature and turn on the fan if the temp is at least
  177. //70
  178. void motor_check(int temp){
  179.   Serial.println();
  180.   Serial.println(temp);
  181.   if(temp >= 70){
  182.     digitalWrite(ledPin,HIGH);
  183.   }
  184.   else{
  185.     digitalWrite(ledPin, LOW);
  186.   }
  187. }
  188.  
  189. //check the morse code input against a given sequence
  190. boolean check_array(int code[],int ref_sequence[]){
  191.   for(int i = 0; i < 5; i++){
  192.     if(code[i] != ref_sequence[i]){
  193.       return false;
  194.     }
  195.   }
  196.   return true;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement