Κυριακή 10 Νοεμβρίου 2013

Κουίζ: Τι κάνει το παρακάτω πρόγραμμα;


int ledPin = 13;
int speakerPin = 8;
char* letters[] = {
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",    // A-I
  ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",  // J-R
  "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."          // S-Z
};

int dotDelay = 75;

void setup()                
{
  pinMode(ledPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
}

void loop()                   
{
  char ch;
  if (Serial.available())     
  {
    ch = Serial.read();       
  
    if (ch >= 'A' && ch <= 'Z')
    {
      flashSequence(letters[ch - 'A']);
    }
   
    else if (ch == ' ')
    {
     delay(dotDelay * 5);     
    }
  }
}

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}


void flashSequence(char* sequence)
{
   int i = 0;
   while (sequence[i] != NULL)
   {
       flashDotOrDash(sequence[i]);
       i++;
   }
   delay(dotDelay * 3);   
}

void flashDotOrDash(char dotOrDash)
{
  digitalWrite(ledPin, HIGH);
  if (dotOrDash == '.')
  {
    playTone(600,dotDelay);
  }
  else
  {
    playTone(600,dotDelay * 3);
  }
  digitalWrite(ledPin, LOW);   
  delay(dotDelay);
}

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου