Saturday, 8 October 2016

2016 JOTA source code for Bike Light

// 2016 JOTA project for Sydney North Region.
// Morse Code Timing - https://en.wikipedia.org/wiki/Morse_code

// Digispark ATTiny85 Boot Loader

#define LED1 0
#define LED2 1
#define LED3 2
#define LED4 5

#define dit_time 200
#define dah_time dit_time * 3

void setup() {
  // put your setup code here, to run once:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  flash(LED1, 500);
  flash(LED2, 500);
  flash(LED3, 500);
  flash(LED4, 500);

  flicker(LED1, 100, 4);
  flicker(LED2, 100, 4);
  flicker(LED3, 100, 4);
  flicker(LED4, 100, 4);

delay(500);

//Morse

// J
dit(); dah(); dah(); dah(); letter_space();

// O
dah(); dah(); dah(); letter_space();

// T
dah(); letter_space();

// A
dit(); dah();letter_space();

word_space();


}
// Sub Routienes

// Morse Dit
void dit() {
  all_on();
  delay(dit_time);
  all_off();
  delay(dit_time);
}

// Morse Dah = 3 x Dit.
void dah() {
  all_on();
  delay(dah_time);
  all_off();
  delay(dit_time);
}

// Letter Space = 3 x Dit
void letter_space() {
  delay(dit_time * 3);
}

// Morse Word Space
void word_space() {
  delay(dit_time * 7);
}

// All LEDs On.
void all_on(){
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
}

// All LEDs Off.
void all_off(){
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
}

// Flash LEDx on for a duration of wait.ms.
void flash(int pin, int wait)
{
  digitalWrite(pin, HIGH);
  delay(wait);
  digitalWrite(pin, LOW);
}

// Flicker LEDx for period wait n times.
void flicker(int pin, int wait, int times)
{
  int i;
  for (i = 0; i <= times; i++) {
    flash(pin, wait);
    delay(wait);
  }
}
Post a Comment