Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Simple Blinking LED Morse Code


  • Please log in to reply
7 replies to this topic

#1 N2MOH

N2MOH

    New Member

  • Members
  • Pip
  • 2 posts

Posted 30 September 2010 - 12:50 PM

Here is a short sample program that sends morse code by blinking the onboard LED
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1 {
	public class Program {
		public static void Main() {
			// write your code here
			OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
			sayhello sho = new sayhello();

			while (true) {
				sho.dosayhello(led);
				
			}

		}
	}

	public class sayhello {
		private const int dittime = 100;
		private const int dahtime = dittime*3;
		private const int intersymtime = dittime * 1;
		private const int interchartime = dittime * 5;
		private const int interwordtime = dittime * 10;

		public void dosayhello(OutputPort led) {
			doWord(led, "HELLO");
			doWord(led, "from");
			doWord(led, "Greg");
		}
		public void doWord(OutputPort led, string ss) {
			ss = ss.ToLower();

			foreach (char cc in ss) {
				switch (cc) {
					case 'a': a(led); break;
					case 'b': b(led); break;
					case 'c': c(led); break;
					case 'd': d(led); break;
					case 'e': e(led); break;
					case 'f': f(led); break;
					case 'g': g(led); break;
					case 'h': h(led); break;
					case 'i': i(led); break;
					case 'j': j(led); break;
					case 'k': k(led); break;
					case 'l': l(led); break;
					case 'm': m(led); break;
					case 'n': n(led); break;
					case 'o': o(led); break;
					case 'p': p(led); break;
					case 'q': q(led); break;
					case 'r': r(led); break;
					case 's': s(led); break;
					case 't': t(led); break;
					case 'u': u(led); break;
					case 'v': v(led); break;
					case 'w': w(led); break;
					case 'x': x(led); break;
					case 'y': y(led); break;
					case 'z': z(led); break;
					case ' ': Thread.Sleep(interwordtime); break;
				}
			}
			Thread.Sleep(interwordtime);
		}

		private void a(OutputPort led) {
			dit(led);
			dah(led);
			Thread.Sleep(interchartime);
		}
		private void b(OutputPort led) {
			dah(led);
			dit(led);
			dit(led);
			dit(led);
			Thread.Sleep(interchartime);
		}
		private void c(OutputPort led) {
			dah(led);
			dit(led);
			dah(led);
			dit(led);
			Thread.Sleep(interchartime);
		}
		private void d(OutputPort led) {
			dah(led);
			dit(led);
			dit(led);

			Thread.Sleep(interchartime);
		}

		private void e(OutputPort led) {
			dit(led);
			Thread.Sleep(interchartime);
		}
		private void f(OutputPort led) {
			dit(led); dit(led); dah(led); dit(led);
			Thread.Sleep(interchartime);
		}
		private void g(OutputPort led) {
			dah(led); dah(led); dit(led);
			Thread.Sleep(interchartime);
		}
		private void h(OutputPort led) {
			dit(led);
			dit(led);
			dit(led);
			dit(led);
			Thread.Sleep(interchartime);
		}
		private void i(OutputPort led) {
			dit(led);
			dit(led);
			Thread.Sleep(interchartime);
		}
		private void j(OutputPort led) {
			dit(led); dah(led); dah(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void k(OutputPort led) {
			dah(led); dit(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void l(OutputPort led) {
			dit(led);
			dah(led);
			dit(led);
			dit(led);
			Thread.Sleep(interchartime);
		}
		private void m(OutputPort led) {
			dah(led);
			dah(led);
			Thread.Sleep(interchartime);
		}
		private void n(OutputPort led) {
			dah(led);
			dit(led);
			Thread.Sleep(interchartime);
		}
		private void o(OutputPort led) {
			dah(led); dah(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void p(OutputPort led) {
			dah(led); dah(led); dah(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void q(OutputPort led) {
			dah(led); dah(led); dit(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void r(OutputPort led) {
			dit(led); dah(led); dit(led);
			Thread.Sleep(interchartime);
		}
		private void s(OutputPort led) {
			dit(led); dit(led); dit(led);
			Thread.Sleep(interchartime);
		}
		private void t(OutputPort led) {
			dah(led); Thread.Sleep(interchartime);
		}
		private void u(OutputPort led) {
			dit(led); dit(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void v(OutputPort led) {
			dit(led); dit(led); dit(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void w(OutputPort led) {
			dit(led); dah(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void x(OutputPort led) {
			dah(led); dit(led); dit(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void y(OutputPort led) {
			dah(led); dit(led); dah(led); dah(led);
			Thread.Sleep(interchartime);
		}
		private void z(OutputPort led) {
			dah(led); dah(led); dit(led); dit(led);
			Thread.Sleep(interchartime);
		}
		private void dit(OutputPort led) {
			led.Write(true);
			Thread.Sleep(dittime);
			led.Write(false);
			Thread.Sleep(intersymtime);
		}
		private void dah(OutputPort led) {
			led.Write(true);
			Thread.Sleep(dahtime);
			led.Write(false);
			Thread.Sleep(intersymtime);
		}
	}
}


#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 30 September 2010 - 01:20 PM

Nice. IMHO using a lookup table would significantly increase readability and simplify the code - something like Scott Hanselman's sample (code is in the middle of the page, just above comments).

#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 30 September 2010 - 02:18 PM

Nice, N2MOH. Thanks for posting this. And welcome to the Netduino community! Chris

#4 N2MOH

N2MOH

    New Member

  • Members
  • Pip
  • 2 posts

Posted 01 October 2010 - 01:21 PM

Nice. IMHO using a lookup table would significantly increase readability and simplify the code - something like Scott Hanselman's sample (code is in the middle of the page, just above comments).


If I had seen Scott's post I wouldn't have bothered to post mine. Scott's code is smaller, thats true, but in my defence I will say that because of cut and paste I bet mine took less time to write.

Next time I will try to add something more interesting.

#5 hari

hari

    Advanced Member

  • Members
  • PipPipPip
  • 131 posts

Posted 01 October 2010 - 01:58 PM

If I had seen Scott's post I wouldn't have bothered to post mine. Scott's code is smaller, thats true, but in my defence I will say that because of cut and paste I bet mine took less time to write.

Next time I will try to add something more interesting.

I hope you continue to share. It's not about code efficiency, it's about sharing our excitement about the Netduino. I think it is just incredibly fun to be able to use Intellisense, and single-step through code that is running on that little board.

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 01 October 2010 - 03:22 PM

If I had seen Scott's post I wouldn't have bothered to post mine. Scott's code is smaller, thats true, but in my defence...

I am sorry for any inconvenience, there is no need to defend yourself - please consider my comment as a fellow developer code review advice Posted Image

#7 segu

segu

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationLima - Peru

Posted 01 October 2010 - 04:55 PM

Me too, I hope you continue to share. As Hari said

... it's about sharing our excitement about the Netduino. ...


And your code worked fine first time, blinking the led as it should be. In the end it is Morse ... dit dah sound or light ;)

The "other" code is no 100.00% correct ... it does not blink the led correctly.
At bottom the code

port.Write(true);
if (dotordash == '.')
Thread.Sleep(100); //dot
else
Thread.Sleep(300); //dash
port.Write(false);

needs one more
Thread.Sleep(100);

to blink "MORSEish" :D

Keep posting your code :)

Regards

#8 hari

hari

    Advanced Member

  • Members
  • PipPipPip
  • 131 posts

Posted 02 October 2010 - 06:11 AM

Speaking of fun... check out the opposite of this app: A morse code interpreter! Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.