Brendan Attempts to Make Some Things

MAS.863/4.140

Embedded Programming: Counter

I thought the hard part of this week would be the programming, but it turned out that actually doing week 5 correctly took way more time. In better news, I can now stuff a board fairly quickly, and wrote my first lines of code that run on a micro-controller.

  • Technologies: Roland Mill, Arduino IDE
  • Week: 7

I started out by following the High Low Tech tutorial on programming in Arduino with an ATTiny44, which was very easy to follow. The Arduino IDE makes it very easy to do things like burn the bootloader and run scripts.

After setting up the software, I tried to program my board. Weirdly enough, no matter what code I fed it, the LED would light up. Even more strange, the light would become even brighter when I pressed the button, even though my code did not communicate to Pin 3, the button. This meant that there had to be a short somewhere, but I could not find it. After hours of struggling I re-milled a board. Unfortunately, I zero-ed the z-axis of the board at z-jog, and ended up breaking a the 1/32 inch end mill. I'll be sure to be much more careful in the future. Stuffing the board was a breeze, and it worked without any modifications

New and improved Hello Echo, now with my name

After this, it was time to program the board. I wanted to make a resettable counter, similar to what umpires use in baseball (it's hard to think of something actually useful with 1 LED and 1 Button). Everytime the button is clicked, the counter is incremented, and then played back in reverse binary using a long light for 1 and a short light for 0. For example the number 10, which is 1010 in binary, will be played back as "short-light, long-light, short-light, long-light." If a button is held, then the counter is reset. There were some interesting quirks in using Arduino, which is a kind of C/C++ mongrel. For example, when allocating memory to a pointer, you need to cast the malloc to that type, which is C++ syntax but not C syntax. Additionally, the extrenal string libraries included in the IDE made coding fairly easy. I think I might go back to pure C in the future, but I'm a huge fan of the IDE! You can see the code at the bottom.

The board counting 2-4.
			
/*
  Counter
  Everytime the button is clicked, the counter is incremented, and then
  played back in binary using a long light for 1 and a short light for 0. 
  Holding down buton resets counter to 0. 
  modified 4 November 2015
  by Brendan Bozorgmir
 */
#include stdlib.h


int ledPin = 7;
int buttonPin = 3;
int val = LOW;
int counter = 0;
static int CHAR_BIT = 8;

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin 7 as output
  pinMode(buttonPin, INPUT);      // sets the digital pin 3 as input
}

void loop()
{
  val = digitalRead(buttonPin);   // read the input pin  
  if (val == LOW) {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
    counter++;
    delay(500);
    val = digitalRead(buttonPin);
    if (val == LOW){
      counter = 0;
    }
    else{
      String binRep = String(counter, BIN);
      int len = binRep.length();
      for (int i = len; i > 0; i--){
        char a = binRep[i - 1];
        if  (a == '0') {
          digitalWrite(ledPin, LOW);
          delay(100);
          digitalWrite(ledPin, HIGH);
          delay(100);
          digitalWrite(ledPin, LOW);
          delay(500);
        }
        else {
          digitalWrite(ledPin, LOW);
          delay(100);
          digitalWrite(ledPin, HIGH);
          delay(500);
          digitalWrite(ledPin, LOW);
          delay(500);
        }
      }
    }
    digitalWrite(ledPin, LOW);
  }
  else{
    
  }
}