An easy way to manually set a 16bit number in an Arduino


In summary, a keypad would be quicker to use, but this is simple to implement

Not in summary, read on….

Note: scroll down if you want to look at the test code.

Setting a 16bit binary number that needs to be viewed in hexadecimal worked well (third row of display in image right).

Although, as the pot has to select one of 256 locations over ~270°, a 45 or 50mm knob would make fine-grain selecting easier.

Setting a 16bit binary number that needs to be viewed in decimal (second row) did not work so well as the upper byte potentiometer affects all five decimal digits.

It is not completely cumbersome:
1, turn the lower pot right down
2, turn the upper pot to its nearest value just below the desired number
3, finish with the lower pot.
But it is not ideal (displaying the number and 255-plus-the-number would speed step 2). Later: yes it did.

Having three pots, each delivering one-in-a-hundred and handling two decimal digits each, would be far better, and only need small diameter knobs (30mm?) as steps would be ~2.5° rather than ~1°. But three pots is tending to madness, maybe? (I might still try it….)

Arduino note: After some debugging, I had learned something important about using the I2C LCD library:

Using ‘lcd.print’ left-justifies the displayed numbers, so when over-writing decreasing numbers, a trail of least-significant digits gets left to the right of the displayed number – hence the need to write five blank locations before writing the new numbers in the code below (strictly, it could have been four blanks for the hex number).

A ‘sprintf’ instruction would apparently have the flexibility do away for the need to pre-blank the number’s landing spot. (BTW, sprintf does not work for floating point numbers in Arduino)

The code is listed below
BTW – More for just looking at, as it has got all sorts of hidden odd characters added by WordPress and the ‘wrong’ spaces, so it will not run if you just copy and paste without first cleaning it up. Also, I did edit it a bit without re-testing before I pasted it here. Anyway…

// Engineer in Wonderland May2022
// Testing setting 16bit numbers using twin potentiometers
// on a four line 20digit LCD with an I2C backpack
// One pot sets low byte, the other the high byte
// Displays set value in decimal and in hexadecimal
// LCD I2C code derived from from Arduino_uno_guy example                   |

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // initialize the liquid crystal library
// 1st parameter is the I2C address
// 2nd parameter is how many rows are on your screen
// 3rd is how many columns are on your screen

// Define variables
// note: potentiometers have outside leads to ground and +5V
unsigned int LoBytePot = A8; // potentiometer wiper to ProMicro pin8
unsigned int HiBytePot = A9; // potentiometer wiper to ProMicro pin9
unsigned int LoByteVal = 0;   // variable is given low byte wiper position in 10bits
unsigned int HiByteVal = 0;   // variable is given high byte wiper position in 10bits
unsigned int WordVal = 0;

void setup() {
// Serial.begin(9600);           //  setup serial if debug needed
lcd.init();              //initialize lcd screen
lcd.backlight();   // turn on screen backlight
}

void loop() {
// read potentiometers and divide each value by 4
LoByteVal = analogRead(LoBytePot);  // read the low byte pot
LoByteVal = LoByteVal >> 2; // divide by 4 to make it one part in 256
HiByteVal = analogRead(HiBytePot);  // read the high byte pot
HiByteVal = HiByteVal >> 2; // divide by 4 to make it one part in 256 (dumps LSBs)

// make value pair into one 16bit binary word
HiByteVal = HiByteVal << 8; // multiply the now 1 in 265 high byte by 256
WordVal = HiByteVal + LoByteVal; // should now have a 16bit number set by two potentiometers

delay(200);  //don’t want it looping too fast

// display the numbers
lcd.setCursor(0,0);   // tell the screen to write on the top row
lcd.print(“16 bit number is…”); // this only really needs running once
lcd.setCursor(0,1);   // tell the screen to write on the second row
lcd.print(”     “);        // clear where the digits are going to delete residual characters
lcd.setCursor(0,1);  // tell the screen to write on the second row
lcd.print(WordVal,DEC); // write the decimal version of 16bit binary word
lcd.setCursor(0,2);  // tell the screen to write on the third row
lcd.print(”     “);        // clear where the digits are going to delete residual characters
lcd.setCursor(0,2);  // tell the screen to write on the third row
lcd.print(WordVal,HEX); // write the hex version of 16bit binary word
}



Source link

We will be happy to hear your thoughts

Leave a reply

KPIM
Logo
Enable registration in settings - general
Shopping cart