Gratefully, happily, and amazingly providing solutions for your needs.
Products > Neckerchief Slide Kit > Neckerchief Slide Kit Code

This code was written for the Arduino. It was ported for use with the ATTiny using the arduino-tiny cores.

#include <ctype.h>
#include <stdio.h>

#define ledPin 4
#define speakerPin 1
#define dit 120
#define dah 360
volatile int _DummyVar = 0;
volatile int _Saying = 0;
char _Letter;
int _Number;

int alpha[26] = {1200,  // A    0     This is where the morse characters
                 2111,  // B    1     are stored. A 1 represents a dit, a 
                 2121,  // C    2     2 represents a dah, and the 0's are 
                 2110,  // D    3     place holders.
                 1000,  // E    4
                 1121,  // F    5
                 2210,  // G    6
                 1111,  // H    7
                 1100,  // I    8
                 1222,  // J    9
                 2120,  // K    10
                 1211,  // L    11
                 2200,  // M    12
                 2100,  // N    13
                 2220,  // O    14
                 1221,  // P    15
                 2212,  // Q    16
                 1210,  // R    17
                 1110,  // S    18
                 2000,  // T    19
                 1120,  // U    20
                 1112,  // V    21
                 1220,  // W    22
                 2112,  // X    23
                 2122,  // Y    24
                 2211,  // Z    25
                };

// These are the mesages that it will play. If you can reprogram the chip, just change these.
char *_Msg0 = "SOS";
char *_Msg1 = "CQ CQ de a Boy Scout.";
char *_Msg2 = "Be prepared";
char *_Msg3 = "Do a good turn daily";
char *_Msg4 = "A Scout is Trustworthy Loyal Helpful Friendly Courteous Kind Obedient Cheerful 
               Thrifty Brave Clean and Reverent";
char *_Msg5 = "On my honor I will do my best to do my duty to God and my country and to obey the 
               Scout Law to help other people at all times To keep myself physically strong 
               mentally awake and morally straight";

// This only runs on startup. It configures the pins as outputs.
//
void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
    
  // This method creates an interrupt. When you push the button, the method StopPlay is 
  // called immediately. The program then resumes from where it left off.
  attachInterrupt(0, StopPlay, FALLING);
}

// This runs continuously after setup() until power off. 
// 
void loop()
{
  PlayMsg(_Saying);
  delay(dit*7);    
}
  
void PlayMsg(int msgNumber)
{
  char *msg;                           // This temporarily stores the message.
  switch(msgNumber)                    // This writes the permanent messages to the temperary one.
  {
    case 0:
      msg = _Msg0;
      break;
    case 1:
      msg = _Msg1;
      break;
    case 2:
      msg = _Msg2;
      break;
    case 3:
      msg = _Msg3;
      break;
    case 4:
      msg = _Msg4;
      break;
    case 5:
      msg = _Msg5;
      break;
  }
      
  // This for loop iterates over every letter of the message.
  //
  for(int t = 0; t < strlen(msg); t++)  
  {
    // First it checks if the button has been pushed and the message incremented.
    if(_DummyVar)           
    {
      Increment();
      _DummyVar = 0;                   // It resets.
      break;                           // And returns to the beginning of loop().
    }
    else
    {
      // This segment of code returns _Number with the ASCII number of the letter.
      _Letter = msg[t];  
        _Letter = toupper(msg[t]);
        _Number = _Letter;
        
      if(_Number >= 65)
        _Number = _Number - 65;        // A is 65 in ASCII so now _Number 
                                       // corresponds to alpha[].
  
      if(_Number == 32)                // ASCII 32 is a space.
      {
        digitalWrite(ledPin, LOW);
        analogWrite(speakerPin, 0);
        delay(dah*2);
      }
      else
      {
        PlayNum(alpha[_Number]);       // Here is actually plays the letter.  
      }
  
    }
  }
}
  
void PlayNum(int morseNum)
  {
    int i;
    char charNum[5];
    itoa(morseNum, charNum, 10);      // This takes the number from alpha[] and makes it a string. By 
                                      // making it a string we can take individual characters.
    
    for(i=0; i < 4; i++)              // In this for loop we get characters representing dits or dahs.
    {
      switch(charNum[i]) 
      {
      case '0':
        break;
        case '1':
          digitalWrite(ledPin, HIGH);  // This turns on the LEDs.
          tone(speakerPin, 440);       // This plays an A with the buzzer.
          delay(dit);                  // Wait for a dit. 120 ms in this case.
          digitalWrite(ledPin, LOW);   // These two lines of code turn things back off.
          noTone(speakerPin);
          delay(dit);                  // Now wait for the spaceing between letters.
          break;
        case '2':
          digitalWrite(ledPin, HIGH);  // This is the same thing, but it plays a dah.
          tone(speakerPin, 440);      
          delay(dah);
          digitalWrite(ledPin, LOW);
          noTone(speakerPin);            
          delay(dit);
      }      
      if(i == 3)                       // This means that the letter is over. Wait for the proper
                                       // length of time between letters.
      {
        delay(dah);
      }
    }
    
  }

// This is called when the button is pressed.
//  
void StopPlay()
  {
    _DummyVar++;                       // Increment this variable. Later, if it is greater than 
                                       // 0 we increment sayings.
  }

void Increment()
  {
    if(_Saying == 5)                   // If we're on the last saying, go to the first.
    {
      _Saying = 0;
    }
    else                               // Else, go to the next.
    {
      _Saying ++;
    }
  
    delay(dit*2);
    for(int j = 0; j < _Saying + 1; j++)  // This flashes and buzzes a different tone to 
                                          // represent the number of the saying.
    {
      digitalWrite(ledPin, HIGH);
      tone(speakerPin, 700);
      delay(dit/2);
      digitalWrite(ledPin, LOW);
      noTone(speakerPin);
      delay(dit);
    }
    delay(dah*2);
  }
      
Contact Us