| Arduino Forum ::  Members :: HULK
 Show Posts Pages: 1 2 [3] 4 5 ... 14
 31 Forum 2005-2010 (read only) / Syntax & Programs / Re: Arithmetic Operator question on: October 22, 2008, 04:27:13 pm m & mI have tested and edited my code to get some good results, but...I did try the long command and it was returning me negative numbers all the time. Could it be the printDouble function i am using or just me as usuall   .One code below that can be used with serial.print and one with the KS0108 library.This one will run to 0.3 and then just jump to -1Code:  int X = 5;//////////////////////////////////////////////////////////////////////////void printDouble( double val, unsigned int precision){    Serial.print (int(val));  //prints the int part    Serial.print("."); // print the decimal point    unsigned int frac;    if(val >= 0)      frac = (val - int(val)) * precision;    else       frac = (int(val)- val ) * precision;    int frac1 = frac;    while( frac1 /= 10 )        precision /= 10;    precision /= 10;    while(  precision /= 10)        Serial.print("0");    Serial.println(frac,DEC) ;}//////////////////////////////////////////////////////////////////////////void setup(){Serial.begin(9600);}void loop(){  float GX = analogRead(X)*0.0048828125 - 1.66;  GX = GX / 0.333;  printDouble(GX, 4);  delay(100);    }This one will go all the way from 0-0.99 and then suddenly go to correct negativ value of -1.0.....Code:#include #include   int X = 5;  ///////////////////////////////////////////////////////////////////////////////////// printDouble void printDouble( double val, byte precision){  // prints val with number of decimal places determine by precision  // precision is a number from 0 to 6 indicating the desired decimial places  // example: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)  GLCD.PrintNumber( (long)val);  //prints the int part  if( precision > 0) {    GLCD.PutChar('.');    unsigned long frac;    unsigned long mult = 1;    byte padding = precision -1;    while(precision--)       mult *=10;           if(val >= 0)      frac = (val - int(val)) * mult;    else      frac = (int(val)- val ) * mult;    unsigned long frac1 = frac;    while( frac1 /= 10 )      padding--;    while(  padding--)      GLCD.PutChar('0');    GLCD.PrintNumber(frac) ;  }}float drawSine(int angle){  float sine;//    if(angle <= 0)           sine = sin(PI / 180 * angle);  return sine;              }//////////////////////////////////////////////////////////////////////////void setup(){  GLCD.Init(NON_INVERTED);GLCD.SelectFont(Arial_14);Serial.begin(9600);}void loop(){  float GX = analogRead(X)*0.0048828125 - 1.66;  GX = GX / 0.333;  GLCD.FillRect(04, 45, 100, 15, WHITE);  GLCD.GotoXY(05, 45);   printDouble(GX, 2);  delay(100);    }Good to have you back mem and thanks for the fast reply mikalhart!
 32 Forum 2005-2010 (read only) / Syntax & Programs / Re: Arithmetic Operator question on: October 22, 2008, 03:01:52 pm mikalhart,You are correct, in my case it is defined so the value is being 0-1023, i can see the value of let us say 250 just by doing a simple serial print command.In my case the pin is: int Z = 5;Any other thoughts or did i missunderstand your reply ?
 33 Forum 2005-2010 (read only) / Syntax & Programs / Arithmetic Operator question on: October 22, 2008, 02:34:29 pm Code:int Z;float GZ = analogRead(Z)*0.0048828125 - 1.66;Hope somebody can help me to understand what is wrong with the above piece of code.As you know an analog reading is a number between 0-1023.So let us assume that analogRead(Z) is at a given time 250, that in multiplication with 0.0048828125 will return 1.220703125.Then 1.220703125 - 1.66 should return -0.439296875. But it does return a positive number of 0.439296875.The intresting part to me is that if we go below -1.000 then the value return from the same code will return the correct negativ value of -1.000.  :-?This is a part of a bigger code that calculates a analog g-sensor value in to volts and then to g force. Therfore 5/1024 = 0.0048828125 for volt calculation.
 34 Forum 2005-2010 (read only) / Interfacing / Convert serial hex to decimal on: September 13, 2008, 02:14:46 am Is there a command that can convert a serial reply from hex to a decimal number?I have tried the search function and it seems as a code is needed, no direct arduino command.A typical reply from my serial device will be XXXXOF28, first four signs are of no interest but also in hex form. So actually i need to filter out the last four digits and then convert to one decimal number. Is it also possible to control the amount of signs to be converted from the right hand side of the hex number reply so only two,four or three signs are read if needed and allways ignore the amount of signs in the start of the hex number reply?Simple questions for some of you, but for me  :-/
 35 Forum 2005-2010 (read only) / Interfacing / Timer on a KS0108 display on: September 13, 2008, 01:13:02 am Just wanted to share a timer code that will display in the right lower corner.Good to have if you want to keep track of the time passed from start up in a application.Will show, hh:mm:ssI did found that it only will count up to 27 hours something before it goes from 00:00:00 again, hopefully 27 hours timers will be good enough for people.And yes, the code is far from perfect but it works. The time code was found on the forum, thanks to the man that created that.To get more informtion on the used ks0108 library and set up check:http://www.arduino.cc/playground/Code/GLCDks0108Code for clock math to be added before void setupCode:unsigned long current_millis_value = 0;unsigned long previous_millis_value = 0;unsigned long m = 0;unsigned int seconds = 0;unsigned int minutes = 0;unsigned int hours = 0;Code for clock math to be added in void loopCode:  current_millis_value = millis();  m += current_millis_value - previous_millis_value;  seconds += m / 1000;  m = m % 1000;  minutes += seconds / 60;  seconds = seconds % 60;  hours += minutes / 60;  minutes = minutes % 60;  hours = hours % 24;  previous_millis_value = current_millis_value; Code for displaying clock to be added in the void loopCode:GLCD.FillRect(110, 47, 14, 10, WHITE);    if (seconds < 10){  GLCD.GotoXY(117, 47);   GLCD.PrintNumber(seconds);  GLCD.GotoXY(110, 47);   GLCD.Puts("0");  }  else {  GLCD.GotoXY(110, 47);   GLCD.PrintNumber(seconds);  }    GLCD.GotoXY(108, 46);   GLCD.Puts(":");    GLCD.FillRect(94, 47, 13, 10, WHITE);    if (minutes < 10){  GLCD.GotoXY(101, 47);   GLCD.PrintNumber(minutes);  GLCD.GotoXY(94, 47);   GLCD.Puts("0");  }  else {  GLCD.GotoXY(94, 47);   GLCD.PrintNumber(minutes);  }    GLCD.GotoXY(92, 46);   GLCD.Puts(":");    GLCD.FillRect(78, 47, 13, 10, WHITE);    if (hours < 10){  GLCD.GotoXY(85, 47);   GLCD.PrintNumber(hours);  GLCD.GotoXY(78, 47);   GLCD.Puts("0");  }  else {  GLCD.GotoXY(78, 47);   GLCD.PrintNumber(hours);  }