Štěpán Bechynský
Published © Apache-2.0

Dead Drop for Adventure Games

Dead drop for kids' adventure games showing location of treasure using Morse code.

BeginnerShowcase (no instructions)1 hour639
Dead Drop for Adventure Games

Things used in this project

Story

Read more

Code

Morse code

C/C++
It works with ATTiny85
#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>

// Define the I/O port to be used for the LED.
#define GREEN PB0
#define RED PB1

// timing
#define DOT 400
#define DASH 1200
#define PAUSE 400
#define NEW_CHAR_PAUSE 1800

// to make it simple all letters are normalized to 4 items
#define LETTER_SIZE 4

// 0 means no action
#define A {DOT, DASH, 0, 0}
#define B {DASH, DOT, DOT, DOT}
#define C {DASH, DOT, DASH, DOT}
#define D {DASH, DOT, DOT, 0}
#define E {DOT, 0, 0, 0}
#define F {DOT, DOT, DASH, DOT}
#define G {DASH, DASH, DOT, 0}
#define H {DOT, DOT, DOT, DOT}
#define I {DOT, DOT, 0, 0}
#define J {DOT, DASH, DASH, DASH}
#define K {DASH, DOT, DASH, 0}
#define L {DOT, DASH, DOT, DOT}
#define M {DASH, DASH, 0, 0}
#define N {DASH, DOT, 0, 0}
#define O {DASH, DASH, DASH, 0}
#define P {DOT, DASH, DASH, DOT}
#define Q {DASH, DASH, DOT, DASH}
#define R {DOT, DASH, DOT, 0}
#define S {DOT, DOT, DOT, 0}
#define T {DASH, 0, 0, 0}
#define U {DOT, DOT, DASH, 0}
#define V {DOT, DOT, DOT, DASH}
#define W {DOT, DASH, DASH, 0}
#define X {DASH, DOT, DOT, DASH}
#define Y {DASH, DOT, DASH, DASH}
#define Z {DASH, DASH, DOT, DOT}

// here you can create message to show
#define MESSAGE_SIZE 5
const int message[MESSAGE_SIZE][LETTER_SIZE] = {H, E, L, L, O};

int main(void) {
	// Set the LED port number as output.
	// The DDRB is the data direction for port B.
	// What this does is:
	//  - shifts the "1" on left, N-times depending on the LED1_PORT value; and ...
	//  - does bitwise "OR" with the value in the port register DDRB.
	DDRB |= (1 << GREEN) | (1 << RED);

	// Begin an infinite loop. This is how most programs work.
	while (1) {
		// Set the LED port bit to "1" - LED will be turned on.
		// This is bitwise "OR" operation with "1".
		// Green LED shows start of message
		PORTB |= (1 << GREEN);
		_delay_ms(1000);
		PORTB &= ~(1 << GREEN) ;
		_delay_ms(400);

		int i, j;
		for (i = 0; i < MESSAGE_SIZE; i++)
		{
			for (j = 0; j < LETTER_SIZE; j++)
			{
				PORTB |= (1 << RED);
				// __builtin_avr_delay_cycles expects a compile time integer constant
				if (message[i][j] == DOT)
				{
					_delay_ms(DOT);
				}
				else if (message[i][j] == DASH) 
				{
					_delay_ms(DASH);					
				}				
				PORTB &= ~(1 << RED) ;
				_delay_ms(PAUSE);
			}
			_delay_ms(NEW_CHAR_PAUSE);
		}
	}

	return (0);
}

Credits

Štěpán Bechynský

Štěpán Bechynský

8 projects • 30 followers
Internet of Things, Arduino, Microsoft, ex-Microsoft MVP

Comments