Landing : Athabascau University
  • Blogs
  • Circuit 1B: Potentiometer

Circuit 1B: Potentiometer

Building this circuit was pretty straightforward, I just followed the instructions in the SIK Guide. Instead of using the SIK Guide example code I decided to modify my morse code program from the previous circuit. My modified morse code program allows the potentiometer to affect the timing of the morse code message, so that adjusting the potentiometer causes the message to be displayed at a faster or slower rate. Here is the program:

# define LED_PIN 12 // Pin number attached to LED.
void setup() {
// put your setup code here, to run once:
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
letterA();
longSpace();
letterD();
longSpace();
letterA();
longSpace();
letterM();
longerSpace();
}
int ditDuration(){
//read the position of the pot
return analogRead(A0);
}
void letterA(){
dit();
space();
dah();
}
void letterD(){
dah();
space();
dit();
space();
dit();
}
void letterM(){
dah();
space();
dah();
}
void dit(){
digitalWrite(LED_PIN, HIGH); // Turn on the LED.
delay(ditDuration()); // The duration of a dit is based on the position of the potentiometer.
}
void dah(){
digitalWrite(LED_PIN, HIGH); // Turn on the LED.
delay(3*ditDuration()); // The duration of a dah is 3 times that of a dit.
}
void space(){
digitalWrite(LED_PIN, LOW); // Turn off the LED.
delay(ditDuration()); // The duration of a space is equal to that of a dit.
}
void longSpace(){
digitalWrite(LED_PIN, LOW); // Turn off the LED.
delay(3*ditDuration()); // The duration is 3 times that of a dit, used between characters.
}
void longerSpace(){
digitalWrite(LED_PIN, LOW); // Turn off the LED.
delay(7*ditDuration()); // The duration is 7 times that of a dit, used between words.
}

The main changes from the original morse code program are that ditDuration() was added to return the value from the potentiometer, and multiples of that value were used to adjust the delay time in dit(), dah(), space(), longSpace(), and longerSpace().

Here is a video of the morse code potentiometer program in action:  MorseCodePotentiometer

I did have one difficulty while writing the program. Initially, I only checked the value of the potentiometer once inside loop(). The issue with this is that the rate of blinking of the LED would not change until loop() had restarted. This meant that when I turned the potentiometer on the breadboard, the rate of blinking would not change until the current morse code message was completely finished. To make the LED more immediately responsive to the potentiometer, I put analogRead(A0) inside ditDuration(), and then called ditDuration() in dit(), dah(), space(), longSpace(), and longerSpace(). This means that the potentiometer value is read every time the LED turns on or off, so that when I turn the knob I just have to wait until the LED turns on or off again to see the change in the rate of blinking.