Tinkering with Arduino

I bought an Arduino Uno and I’ve been slowly making my way through the tutorials learning about circuits. I started to stray off the beaten path and made a strange timer thing that prints out button presses on an LCD screen with a few custom created characters (arrows). It’s not the most exciting thing in the world but I’m ecstatic it actually works!

My n00b sketch:

#include 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buttonPin = 8;
int buttonState = 0;
int lastButtonState = 0;
int pressCount = 0;
float timer = 0;
int padding = 0;

//this is a custom arrow up symbol
byte newCharUp[8] = {
 B00000,
 B00100,
 B01110,
 B10101,
 B00100,
 B00100,
 B00100,
 B00000,
};

//this is a custom arrow down symbol
byte newCharDown[8] = {
 B00000,
 B00100,
 B00100,
 B00100,
 B10101,
 B01110,
 B00100,
 B00000,
};

void setup() {
  //assign custom characters to (char) 0 and 1
  lcd.createChar(0, newCharUp);
  lcd.createChar(1, newCharDown);
  
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if (buttonState != lastButtonState) {
    lcd.clear();
    lcd.print("Button Press: ");
    lcd.print(pressCount);
    if (buttonState == HIGH) {  
      lcd.setCursor(0, 1);
      lcd.print((char)0);
      lcd.print("UP");
    } else {
      lcd.setCursor(0, 1);
      lcd.print((char)1);
      lcd.print("DOWN");
    }
    lastButtonState = buttonState;
    if (buttonState == HIGH) {
      pressCount++;
    }
  }

  if (buttonState == LOW) {
    int digit = Serial.println(timer);
    //determine timer's total digits to right-justify it on the screen.
    padding = digit - 2;
    lcd.setCursor(15-padding,1);
    lcd.print(timer);
    lcd.setCursor(15,1);
    lcd.print("s");
    delay (100);
    timer+=0.1;
  } else {
    timer = 0;
  }
}
Advertisements

2 thoughts on “Tinkering with Arduino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s