14-Segment Alpha Display - show integer

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
phrazelle
 
Posts: 31
Joined: Mon Jan 26, 2015 11:39 pm

14-Segment Alpha Display - show integer

Post by phrazelle »

Ok, like basically everyone who ever makes a first post on here, I am relatively new to Arduino programming and have a pretty basic question that is probably already answered in the forum. I looked and didn't find what I was looking for, but it's probably there. I took some light programming classes in early college before shifting to design. I jumped into Pi and Arduino based projects last November for whatever reason. That being said, I am versed enough to be dangerous, but don't fully understand how I'm communicating with my peripherals at points. I want to understand what I'm doing and not just get a "type this code and you're good." THAT being said, I would definitely just take the answer too... ;)

So here is my project:

I saw this pop-a-shot-esque basketball hoop on Instructables and liked the concept, but wanted to build one with a cleaner design. I picked up a door-hanging hoop with a clear backboard and am mounting this 14-segment alphanumeric display behind the board at the top center, connected to an Arduino Micro, pulling data from this distance sensor.

Hardware wise, I have the following setup:

Arduino Micro
5v > 5v rail
GND > GND rail
A0 > distance sensor (out)
2/SDA > alpha SDA
3/SCL > alpha SCL

Display Sensor
OUT > adruino micro A0
VIN > 5v rail
GND > GND rail

14-Segment Alphanumeric Display
Vi26 > 5v rail
VCC > 5v rail
GND >GND rail
SDA > adrunio micro 2/SDA
SCL > adruino micro 3/SCL

Code-wise, I figure "create an integer, every time the display trigger trips add 1, always display that integer on the alpha display." Easy peasy.

Ok, don't judge, and all the commenting was so I could keep track of what I believed was going on...

Code: Select all

//include libraries
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

//create Adafruit Alphanumeric Display object
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

//define integers
int sensorPin = A0;
int sensorValue = 0;
int score = 1;

void setup() {
  
  //set sensor pin to INPUT
  pinMode(sensorPin, INPUT);
  //set BAUD rate
  Serial.begin(9600);  
  //start display
  alpha4.begin(0x70);
  //write zeros to all positions
  alpha4.writeDigitAscii(0, '0');
  alpha4.writeDigitAscii(1, '0');
  alpha4.writeDigitAscii(2, '0');
  alpha4.writeDigitAscii(3, '0');
  //display newly written values
  alpha4.writeDisplay();

}

//define spaces for display buffer?
char displaybuffer[4] = {' ', ' ', ' ', ' '};

void loop() {
  
  //declare character array
  char b[5];
  //declare string
  String str;
  //convert score integer to string
  str=String(score);
  //pass value of string to array
  str.toCharArray(b,5);
  //check sensor to see if voltage is LOW
  sensorValue = analogRead(sensorPin);
  if (sensorValue < 200) {
    score++;
    Serial.write(b);
    delay(1500);
  }

}
This is where I stopped last time. The counter works. When the display sensor is tripped (voltage goes low,) the 'score' integer gets one added to it and then it waits long enough to not get a phantom reading. Now I just want to make the integer 'score' show up on the display always as soon as the loop starts.

So I don't understand a few things.

1 - What's the deal with converting integers > strings > character arrays? I mean, why can't the integers just be happy with who they are, am I right? Who are these displays to demand otherwise? (in my best Jerry Seinfeld impersonation)
2 - What is the base information around writing data to displays like this, opposed to the simplified version used with this backpack (I saw a bunch of similar commands used for other projects/displays so I figure this is part of C++?)
3 - (this one probably comes with knowledge of #2) What is the base information around writing data to this specific backpack more-so than what is listed on the tutorial page?
4 - If I just want this to work, what's the code to tell the display "alphadisplay.blah blah(scoreinteger)"

Seriously, I would love actual direction into understanding the elements I am trying to access here, but per #4 would just take the answer. If anyone wants to recommend online resources I am all about reading.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: 14-Segment Alpha Display - show integer

Post by adafruit_support_bill »

Deep down inside, they are all just bits and bytes. The question is how do you interpret them.
Signed integers are a two's complement representation of a number: http://en.wikipedia.org/wiki/Two%27s_complement
Characters are unsigned integers (8-bit in the Arduino world) used to represent characters in the ASCII character set.
http://www.asciitable.com/

There are dozens of ways to convert between them and plenty of good reference sources and forums dealing with the C++ language or programming in general. Here are a couple:
http://www.cplusplus.com/forum/general/103477/
http://stackoverflow.com/questions/4629 ... -character

Writing to these displays, you can either have explicit control over the state of each and every segment of each character position unsing writeDigitRaw(), Or you can use writeDigitAscii() to convert a character code into the segments required to display the associated ASCII character.

If you have an integer called 'number' between 0 and 9, you can convert it to an ASCII character for display using something like:

Code: Select all

alpha4.writeDigitAscii(4, number + '0');
For numbers between 0 and 99, something like this should work:

Code: Select all

alpha4.writeDigitAscii(4, (number%10) + '0');
alpha4.writeDigitAscii(3, (number/10) + '0');

User avatar
phrazelle
 
Posts: 31
Joined: Mon Jan 26, 2015 11:39 pm

Re: 14-Segment Alpha Display - show integer

Post by phrazelle »

Thank you for all the resources! I cleaned up my code a bit and used the writeDigitAscii, but needed to switch the digit positions to 0 1 2 3 instead of 1 2 3 4. It works like a charm!

Code: Select all

//include libraries
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

//create Adafruit Alphanumeric Display object
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

//define integer
int score = 0;

void setup() {
  
  //set sensor pin to INPUT
  pinMode(A0, INPUT);
  //set BAUD rate
  Serial.begin(9600);  
  //start display
  alpha4.begin(0x70);
  //write zeros to all positions
  alpha4.writeDigitAscii(0, '0');
  alpha4.writeDigitAscii(1, '0');
  alpha4.writeDigitAscii(2, '0');
  alpha4.writeDigitAscii(3, '0');
  //display newly written values
  alpha4.writeDisplay();

}

void loop() {

  //add 1 to the score if sensor voltage is LOW
  if (analogRead(A0) < 200) {
    score++;   
 
  //write score to display data
  alpha4.writeDigitAscii(3, (score%10) + '0');
  alpha4.writeDigitAscii(2, (score%100/10) + '0');
  alpha4.writeDigitAscii(1, (score%1000/100) + '0');
  alpha4.writeDigitAscii(0, (score/1000) + '0');
  
  //push display data to display
  alpha4.writeDisplay();
 //wait 1.5 sec to eliminate phantom reads
  delay(1500);
  }

}

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”