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 & m

I 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  smiley .
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 -1
Code:

  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 <Arial14.h>
#include <ks0108.h>

  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:ss
I 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/GLCDks0108

Code for clock math to be added before void setup
Code:
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 loop
Code:
 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 loop
Code:
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);
  }
36  Forum 2005-2010 (read only) / Interfacing / Re: Decimal number on a 4x20 Hitachi LCD on: September 02, 2008, 07:34:10 am
Solow,

Let us do this in English so more people will get a way of reading and also correcting there code if needed.
Remember that this code was created by Mem and i only adapted it to my code so that i could print the turbo presure decimal number on a LCD.

So try to enter this code in the setup area or just before so the doublePrint function will work, this will print a number in a decimal form.

Code:
void printInt(long n){
   byte buf[10];  // prints up to 10 digits  
   byte i=0;
   if(n==0)
         lcd.print('0');
   else{
       if(n < 0){
        lcd.print('-');
            n = -n;
       }
     while(n>0 && i <= 10){
         buf[i++] = n % 10;  // n % base
         n /= 10;   // n/= base
       }
       for(; i >0; i--)
             lcd.print((char) (buf[i-1] < 10 ? '0' + buf[i-1] : 'A' + buf[i-1] - 10));        
   }
}

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)

  printInt( (long)val);  //prints the int part
  if( precision > 0) {
    lcd.print('.'); // print the decimal point
    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--)
      lcd.print('0');
    printInt(frac) ;
  }
}
 


Then use printDouble command in the loop area.

Code:
 
  float c = analogRead(analogPin0) ; //reading the value from a analog pin
  lcd.cursorTo(1,6); //adjust it to your position
  printDouble(c,2);


Try it out, remember that i also am a rooky on this and that i did a lot of try and error before geting it right.
Hope it works.
If not Post your code and maybe we can get right in the end.
37  Forum 2005-2010 (read only) / Interfacing / Re: Decimal number on a 4x20 Hitachi LCD on: August 09, 2008, 05:37:51 pm
Yes, finally i sorted that out thanks to your help Mem.
But i had to do it with p=c inside the statement, otherwise it would after two seconds again not see p as greater then c. So my code ended up like this and works great now.

Code:
void loop() {  
  
  static float p;
  
  float c = analogRead(analogPin0) * 0.00491;
  lcd.cursorTo(1,6);
  printDouble(c,2);

  if (c > p){
  lcd.cursorTo(1,16);
  printDouble(c,2);
  p= c;
  }
  
38  Forum 2005-2010 (read only) / Interfacing / Re: Decimal number on a 4x20 Hitachi LCD on: August 09, 2008, 11:14:40 am
Your code works fine, but i am finding it hard to tun it with the If comando.
Listed code below will allways print the c value when executing the if comando. It will never see p as greater then c.
Something i am missing ?

Code:
void loop() {  
  
  float c = analogRead(analogPin0) * 0.00491;
  lcd.cursorTo(1,6);
  printDouble(c,2);

if (c > p){
  lcd.cursorTo(1,16);
  printDouble(c,2);
  float p = c;
  }

39  Forum 2005-2010 (read only) / Interfacing / Re: Decimal number on a 4x20 Hitachi LCD on: August 07, 2008, 02:24:38 pm
Mem,

I am trying to run your code but can not really get what command i should use in the void loop() area to print a value?

This value will not be constant by the way.
40  Forum 2005-2010 (read only) / Interfacing / Re: Decimal number on a 4x20 Hitachi LCD on: August 07, 2008, 01:28:24 pm
I can see that it is possible and that it is done in the code below, but can not understand the logic in it

Code:
/* Analog in to LCD 4 bits
 * ---------
 * Adapted from the "analog_read_send" and "lcd_8bits" tutorials.  
 * This example uses 4 less pins on the Arduino than the 8 bit example.
 * It will take a reading from a 'K' Type thermocouple ice point reference chip  
 * on Analog Input 2 and display the temperature in degrees Centigrade on the LCD.  
 * One can also set a target temperature for turning a relay off, say for a heater,
 * at a given setpoint temperature. This is done on digital pin 4.
 *
 * These are the pins used on the LCD:
 *  
 * - DI(register select), RW, DB4..DB7, Enable (7 in total)
 *
 * the pinout for LCD displays is standard and there is plenty
 * of documentation to be found on the internet.
 *
 * 2006, Dave Sopchak  glasspusher at outofoptions dot net
 *
 */
  
int DI = 12; // register select
int RW = 11;
int DB[] = {7, 8, 9, 10};
int Enable = 6;
 
int temperaturePin = 2;    // select the input pin for the temperature
int ledPin = 13;           // pin for the LED
 
void tickleEnable()
{
 // send a pulse to enable
 digitalWrite(Enable,HIGH);
 delayMicroseconds(1);  // pause 1 ms according to datasheet
 digitalWrite(Enable,LOW);
 delayMicroseconds(1);  // pause 1 ms according to datasheet
}  
 
void cmdWriteSet()
{
 digitalWrite(Enable,LOW);
 delayMicroseconds(1);  // pause 1 ms according to datasheet
  digitalWrite(DI,0);
  digitalWrite(RW,0);
}
void LcdCommandWrite(int value)  
{
 int i = 0;
 
  for (i=DB[3]; i >= DB[0]; i--) // high nybble first
  {
    digitalWrite(i, value & 128);
    value <<= 1;
  }
    cmdWriteSet();
    tickleEnable();
 
  for (i=DB[3]; i >= DB[0]; i--) // low nybble next
  {
    digitalWrite(i, value & 128);
    value <<= 1;
  }
    cmdWriteSet();
    tickleEnable();
}
 
void LcdDataWrite(int value)  
{
 int i = 0;
  
 digitalWrite(DI, HIGH);
 digitalWrite(RW, LOW);
  
  for (i=DB[3]; i >= DB[0]; i--) // high nybble first
  {
    digitalWrite(i, value & 128);
    value <<= 1;
  }
    tickleEnable();
 
  for (i=DB[3]; i >= DB[0]; i--) // low nybble next
  {
    digitalWrite(i, value & 128);
    value <<= 1;
  }
    tickleEnable();
}
 
void setup (void)  
{
 int i;
 for (i=Enable; i <= DI; i++)  
   pinMode(i,OUTPUT);
 
 delay(100);
 // initiatize lcd after a short pause
 // needed by the LCDs controller
 LcdCommandWrite(0x28);  // function set:
  delay(64);             // 4-bit interface, 2 display lines, 5x7 font
                        // other interaces:
                        // 0x20 = 4 bit, 1 display line
 
 LcdCommandWrite(0x28);  // function set:
  delay(64);             // 4-bit interface, 2 display lines, 5x7 font
  
  LcdCommandWrite(0x06);  // entry mode set:
                        // increment automatically, no display shift
 delay(20);                      
 LcdCommandWrite(0x0E);  // display control:
                        // turn display on, cursor on, no blinking
 delay(20);                      
 LcdCommandWrite(0x01);  // clear display, set cursor position to zero  
 delay(100);                      
 LcdCommandWrite(0x80);  // display control:
                        // turn display on, cursor on, no blinking
 delay(20);      
}
 
void loop (void)  
{
  int i, val = 0;
  
  for(i = 0; i < 20; ++i)
  {
       val += analogRead(temperaturePin);    // read the value from the sensor
       delay(50);
  }
  
  val /= 4.06;      // conversion value to millivolts
  
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(500);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  
  if(val > 175 * 10)   // temperature in deg C times 10, since we're measuring to tenths of a degree
     digitalWrite(4,LOW);
   else
     digitalWrite(4,HIGH);
 
    LcdCommandWrite(0x02);  // set cursor position to zero  
  delay(10);                      
  firstDisplay(val);
}
 
void firstDisplay(int value)
{
  int first,second, third, fourth;
  
  first = value / 1000;    //  
  second = (value - 1000 * first)/ 100;
  third = (value - 1000 * first - 100 * second)/ 10;
  fourth = (value - 1000 * first - 100 * second - 10 * third);
  
     LcdDataWrite('T');
     LcdDataWrite('e');
     LcdDataWrite('m');
     LcdDataWrite('p');
     LcdDataWrite(' ');
     LcdDataWrite('=');
     LcdDataWrite(' ');
 
     LcdDataWrite(value > 999 ? first + 48 :  ' ');  // begin onscreen
     LcdDataWrite(value > 99 ? second + 48 : ' ');
     LcdDataWrite(third + 48);
     LcdDataWrite('.');
     LcdDataWrite(fourth + 48);
 
     LcdDataWrite(' ');
     LcdDataWrite('C');
     LcdDataWrite(' ');
     LcdDataWrite(' ');
}

Have also found the command "itoa()" but can it be used with decimals?
41  Forum 2005-2010 (read only) / Interfacing / Decimal number on a 4x20 Hitachi LCD on: August 07, 2008, 01:15:24 pm
Hi,

So i need to display a decimal value on a Hitachi LCD in 4 bit mode.
Have been reading true some old posts using the search function but can not find a complete and proper code example. Or at least not one i can understand, sorry...

So i am reading a value from a analog port from 0-1023 and want convert and display that in volts with at least two decimals such as example 4,54 volts.
Well, never mind the converting part, just need to understand what is needed to print the decimal form of value.
Is it possible to do with allready existin library such as the neillzero library?

I did manage to do that with serial communication, see code example below.
Code:
int analogPin = 0;
double z;


 
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(){
  
     z = analogRead(analogPin) * 0.00491;
     printDouble(z,100);  // two decimal places
     delay(100);
 }

By the way, not my code for the decimal handling, thank you who ever it was i stole it from  smiley-grin.
42  Forum 2005-2010 (read only) / Interfacing / Re: Writing signs on a 4 bit Hitachi LCD on: August 12, 2008, 05:37:31 am
Phil,

Yes that is a good site, that is actually the one i used to find out the listed information in my first post.
The knowledge i lack is how to write a code or what command to use to get it displayed on the LCD.

Thanks,
43  Forum 2005-2010 (read only) / Interfacing / Re: Writing signs on a 4 bit Hitachi LCD on: August 09, 2008, 11:07:45 am
Yes you are correct, found information on it.
Thanks, seems by the way to be to comlicated for my rooky skills.
But i will give it a true in the future when my knowledge is greater...
44  Forum 2005-2010 (read only) / Interfacing / Writing signs on a 4 bit Hitachi LCD on: August 08, 2008, 12:13:51 pm
Another day, another question.

What code can be used to print signs that are not allready specified for the hitachi lcd.
I know as much as this for the sign i want to print:

In decimal:  16,16,16,16,16,16,16
In hex:  0x10,0x10,0x10,0x10,0x10,0x10,0x10
In binary:  %10000,%10000,%10000,%10000,%10000,%10000,%10000

45  Forum 2005-2010 (read only) / Development / Re: new wireless comms module on: July 20, 2009, 01:10:52 am
Thanks for sharing that.
Pages: 1 2 [3] 4 5 ... 14