Closed Swiftnesses closed 4 years ago
What display module are you using for your project...?
Hi,
I believe it's a SH1106 based display...
do you mean the 128x64 OLED module, do you....?
Hi GrannyFanny 🥇
Yes, it's a 128x64 module :)
do you have the spi or i2c version of the board?
/* give this sketch a go (it's actually for displaying the temp values on a whole RANGE of LCD displays and OLEDs {both i2C & SPI...} in the format of bargraph, they are updated real-time, as requested....
LET ME KNOW how you go with it, or ask if any of it isn't clear to you, and Ill do my veryvery best to help you along :)
*/
/! @defined CHAR_WIDTH @abstract Character witdth of the display, expressed in pixeles per character. /
/! @defined BACKLIGHT_PIN @abstract LCD backlight pin definition. @discussion AVR pin used for the backlight illumintation of the LCD. /
/! @defined STATUS_PIN @abstract Status LED indicator. @discussion Activity LED blinking indicating that the system is up. /
/! @defined LOOP_DELAY @abstract Main loop delay. @discussion Main loop delay executing temperature readings and LCD updates. /
/! @defined TEMP_CAL_OFFSET @abstract Temperature calibration offset. @discussion This is the offset value that has to be modified to get a correct temperature reading from the internal temperature sensor of your AVR. /
/! @defined FILTER_ALP @abstract Low pass filter alpha value @discussion This value defines how much does the current reading, influences the over all value. The smaller, the less influence the current reading has over the overall result. /
/*! @defined MIN_TEMP @abstract Minimum temperature range for bargraph
*/
/*! @defined MAX_TEMP @abstract Maximum temperature range for bargraph
*/
extern unsigned int bss_end; extern unsigned int __heap_start; extern void *brkval;
// Initialise LCD module // -----------------------------------------------------------------------------
LiquidCrystal_I2C lcd(0x38);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2, BACKLIGHT_PIN, POSITIVE); const int CONTRAST_PIN = 9; const int CONTRAST = 65;
LiquidCrystal_SR lcd(3,2,TWO_WIRE); // | | // | -- Clock Pin // ---- Data/Enable Pin
LiquidCrystal_SR3W lcd(3, 2, 4); // | | // | -- Clock Pin // ---- Data/Enable Pin
// LCD reference variable LCD *myLCD = &lcd;
// Temperature filter variable static double tempFilter;
/! @const charBitmap @abstract Define Character bitmap for the bargraph. @discussion Defines a character bitmap to represent a bargraph on a text display. The bitmap goes from a blank character to full black. / const uint8_t charBitmap[][8] = { { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x0 }, { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0 }, { 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x0 }, { 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x0 }, { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x0 }, { 0xe, 0x11, 0x11, 0x11, 0xe, 0, 0, 0 }, { 0x6, 0x9, 0x9, 0x6, 0x0, 0, 0, 0} };
/!
@function
@abstract Return available RAM memory
@discussion This routine returns the ammount of RAM memory available after
initialising the C runtime.
@param
@result Free RAM available.
/
static int freeMemory()
{
int free_memory;
if((int)brkval == 0) free_memory = ((int)&free_memory) - ((int)&bss_end); else free_memory = ((int)&free_memory) - ((int)__brkval);
return free_memory; }
/! @function @abstract Returns AVR328p internal temperature @discussion Configures the ADC MUX for the temperature ADC channel and waits for conversion and returns the value of the ADC module @result The internal temperature reading - in degrees C /
static int readTemperature() { ADMUX = 0xC7; // activate interal temperature sensor, // using 2.56V ref. voltage ADCSRB |= _BV(MUX5);
ADCSRA |= _BV(ADSC); // start the conversion while (bit_is_set(ADCSRA, ADSC)); // ADSC is cleared when the conversion // finishes
// combine bytes & correct for temperature offset (approximate)
return ( (ADCL | (ADCH << 8)) - TEMP_CAL_OFFSET);
}
/! @function @abstract Braws a bargraph onto the display representing the value passed. @discussion Draws a bargraph on the specified row using barLength characters. @param value[in] Value to represent in the bargraph @param row[in] Row of the LCD where to display the bargraph. Range (0, 1) for this display. @param barlength[in] Length of the bar, expressed in display characters. @param start[in] Start bar character @param end [in] End bar character @result None / static void drawBars ( int value, uint8_t row, uint8_t barLength, char start, char end ) { int numBars;
// Set initial titles on the display myLCD->setCursor (0, row); myLCD->print (start);
// Calculate the size of the bar value = map ( value, MIN_TEMP, MAX_TEMP, 0, ( barLength ) * CHAR_WIDTH ); numBars = value / CHAR_WIDTH;
// Limit the size of the bargraph to barLength if ( numBars > barLength ) { numBars = barLength; } myLCD->setCursor ( 1, row );
// Draw the bars while ( numBars-- ) { myLCD->print ( char( 5 ) ); }
// Draw the fractions numBars = value % CHAR_WIDTH; myLCD->print ( char(numBars) ); myLCD->setCursor (barLength + 1, row); myLCD->print (end);
}
/!
@function
@abstract Initialise the HW
@discussion Initialise the HW used within this application: UART, LCD & IOs
@param
@result
/
static void initHW ( void ) { int i; int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));
Serial.begin ( 57600 );
// Hardware initialise // ------------------------------------
//ADCSRA |= (1 << ADEN); // Initialise ADC block (no need done by env)
// Initialise LCD HW: backlight and LCD // -------------------------------------
pinMode(CONTRAST_PIN, OUTPUT); analogWrite (CONTRAST_PIN, CONTRAST);
pinMode ( BACKLIGHT_PIN, OUTPUT ); digitalWrite (BACKLIGHT_PIN, HIGH);
pinMode ( STATUS_PIN, OUTPUT );
myLCD->begin ( 20, 2 ); // Load custom character set into CGRAM // -------------------------------------------------------------------- for ( i = 0; i < charBitmapSize; i++ ) { myLCD->createChar ( i, (uint8_t *)charBitmap[i] ); } }
void setup () { initHW();
Serial.println ( freeMemory () ); myLCD->clear (); myLCD->print ( F("Free Mem: ")); myLCD->print ( freeMemory () ); delay ( 2000 ); myLCD->clear (); myLCD->print (F("Temp:")); myLCD->setCursor ( 8, 0 );
tempFilter = 0; myLCD->print ( readTemperature() ); }
void loop () { int temp; static byte status = 1;
status ^= 1; digitalWrite ( STATUS_PIN, status);
temp = readTemperature(); tempFilter = ( FILTER_ALP temp) + (( 1.0 - FILTER_ALP ) tempFilter);
// Display the information to the LCD myLCD->setCursor ( 8, 0 ); myLCD->print (" "); myLCD->setCursor ( 8, 0 ); myLCD->print ( tempFilter, 1 ); myLCD->setCursor ( 12, 0 ); myLCD->print ( "\x07" ); myLCD->print ("C"); drawBars ( tempFilter, 1, 14, '-', '+' );
delay (LOOP_DELAY); }
/*
This Arduino sketch is to use LM35 to measure temperature
prints the temperature as C, F or K on the LCD1602 or LCD2004 with I2C module (4 wires)
This code has been download from Robojax.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
*/
const int inPin =A0;//can change const int VCC2 =2; const int iteration = 1000; //can change (see video) const float LM35_FACTOR =0.01;// do not change
// start of settings for LCD1602 with I2C
// Set the LCD address to 0x27 for a 16 chars and 2 line display LiquidCrystal_I2C lcd(0x26, 16, 2); // end of settings for LCD1602 with I2C
// the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600);
pinMode(VCC2,OUTPUT); digitalWrite(VCC2, HIGH);
lcd.begin();
lcd.backlight();
lcd.print("Real-Time LM35");
lcd.setCursor(0,1);
lcd.print("Temp: ");
}
// the loop routine runs over and over again forever: void loop() {
lcdDisplay(getTemperature('C'),'C'); delay(2000);
lcdDisplay(getTemperature('F'),'F'); delay(2000);
lcdDisplay(getTemperature('K'),'K');
delay(2000);
if(getTemperature('C') >87) { // do somethig here (watch video) }
// printTemperature('C');
// Serial.println();
// printTemperature('F');
// Serial.println();
// printTemperature('K');
// Serial.println();
// Serial.println();
// Serial.print(" Temperature: ");
// printDegree();
// Serial.print(getTemperature('C'));
// Serial.println();
delay(100);
}
/*
@return returns one of the values above / float getTemperature(char type) { float value; float averageTemperature =0; int sensorValue = analogRead(inPin); float voltage = sensorValue (5.0 / 1023.0); float temperature = voltage / LM35_FACTOR; for(int i=0; i< iteration; i++) { averageTemperature += temperature; }
averageTemperature /=iteration;
if(type =='F')
{
value = averageTemperature *9/5 + 32;//convert to Fahrenheit
}else if(type =='K')
{
value = averageTemperature + 273.15;//convert to Kelvin
}else{
value = averageTemperature;// return Celsius
}
return value ;
}//getTemperature()
/*
@return none
*/
void printTemperature(char type)
{
float value;
float temp = getTemperature(type);
Serial.print(temp);
printDegree();
if(type =='F')
{
Serial.print("F");
}else if(type =='K')
{
Serial.print("K");
}else{
Serial.print("C");
}
}//printTemperature()
/*
@return returns nothing
*/
void printDegree()
{
Serial.print("\xC2");
Serial.print("\xB0");
}
/*
lcdDisplay(35.3,'C');
*/ void lcdDisplay(float value,char symbol) {
for(int i=7; i<16;i++)
{
lcd.setCursor(i,1);
lcd.write(254);
}
lcd.setCursor (7,1); //
lcd.print(value);
lcd.print((char)223);
if(symbol =='F')
{
lcd.print("F");
}else if(symbol =='K')
{
lcd.print("K");
}else{
lcd.print("C");
}
}
Neither use your lib, correct?
Sorry, I just saw this issue. You shouldn't need to fill the display to change the data. Just overwrite the same text and it will display correctly while not producing a "flash"
Hello,
Thanks for the lib - I've been trying to find something that works with my display (this is the first that works perfect). I'm looking to have a screen that displays various moisture levels for plants, in the format of:
Zone 1: 34% Zone 2: 56%
I'd like to update the number in real-time. Currently I'm doing this with a delay and
obdFill(&obd, 0, 1)
, but I'm sure there's a better way (previous with u8g I just set the coordinates and changed the value, cursor?).Any help of advice is greatly appreciated!