Need help with the long variable. I spent the last hours researching how to use it correctly, but no success. I am usually programming in assembly so I am no expert in C/C++. So here is my problem:
I have an OLED screen hooked up and the display and it is showing:
tempChars: 06528.2580
longDeg: 65
longMin: 217044
the first two lines are what I expect, but the 217044 puzzles me. I would expect this to be 282580.
Why can I not copy the characters into the long variable correctly? The method works with smaller variable types like the byte for the longDeg, but I can't seem to get the long variable filled with the correct values. Code is posted below.
I use Arduino 1.8.8 on PC. Just in case this is compiler related.
#include <Wire.h>
#include <U8g2lib.h>
//OLED - interface
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/
U8X8_PIN_NONE);
char tempChars[] = "06528.2580";
float longitude = 0.0;
byte longDeg = 0; //degrees
long longMin = 0; // minutes
//------------------------------------------------------------------------------
void setup() {
u8g2.begin(); //start OLED
}
//------------------------------------------------------------------------------
void loop() {
int i=0;
while(i < 10){
if(i== 0) { } //
if(i == 1){ longDeg = (tempChars[i] - '0')*10; }
if(i == 2){ longDeg += (tempChars[i] - '0'); }
if(i == 3){ longMin = (tempChars[i] - '0')*100000; }
if(i == 4){ longMin += (tempChars[i] - '0')*10000; }
// i == 5 is decimal point
if(i == 6){ longMin += (tempChars[i] - '0')*1000; }
if(i == 7){ longMin += (tempChars[i] - '0')*100; }
if(i == 8){ longMin += (tempChars[i] - '0')*10; }
if(i == 9){ longMin += (tempChars[i] - '0'); }
i++;
}
drawScreen();
} // END loop
//------------------------------------------------------------------------------
void drawScreen(void){
u8g2.firstPage();
do {
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFont(u8g2_font_6x12_tr);
u8g2.setCursor(0, 10); u8g2.print("tempChars: "); u8g2.print(tempChars);
u8g2.setCursor(0, 20); u8g2.print("longDeg: "); u8g2.print(longDeg);
u8g2.setCursor(0, 30); u8g2.print("longMin: "); u8g2.print(longMin);
} while( u8g2.nextPage() );
} // END drawScreen