Probs with int and strings

Well I'm new to c++ but have been coding in flash and vb6 for a year or too. I'm having problems with int / char / strings etc. The code I'm using is below.. I know its a bit messy but I'm learning as I go. What would be really hand would be a brief example of how to convert between the two.. in vb6 all it would be is str(variable) or int(variable) anything similar ?

#include <LCD4Bit_mod.h>

LCD4Bit_mod lcd = LCD4Bit_mod(2);

int inPin = 2; // choose the input pin (for pulse count)
volatile long count = 0;
long currentmils = 0; // variable to hold current mills value
int currentspeed = 0; // holds current speed value (needs to be calibrated)
long currentdelay = 0;
int ledPin = 13; // LED connected to digital pin 13
int ledval = 0;
int batwings = 0;

void setup() {
pinMode(inPin, INPUT); // declare pushbutton as input
currentmils = millis(); // set current millis
Serial.begin(9600); // using serial output so I can monitor whats going on.
pinMode(ledPin, OUTPUT); // sets the digital pin as output
attachInterrupt(0, pulse, FALLING);
lcd.init();
lcd.clear();
}

void loop(){

if ((currentdelay + 100) < millis()) { // runs every second, will shortern time once calibrated
currentdelay = millis();
if (currentspeed > 50) {batwings = 1;}
if (currentspeed < 30) {batwings = 0;}
lcd.cursorTo(1, 0);
lcd.printIn(currentspeed);
lcd.cursorTo(2, 0);
if (batwings == 0 ) {
lcd.printIn("Bat Wings - down");
} else {
lcd.printIn("Bat Wings - up ");
}
//Serial.print(currentspeed);
//Serial.print(" ");
if (ledval == 0 ) {
digitalWrite(ledPin, HIGH);
ledval = 1;
} else {
digitalWrite(ledPin, LOW);
ledval = 0;
}
}
}

void pulse()
{
currentspeed = 1000 / (millis() - currentmils);
currentmils = millis();
}

The closest thing in C to str(val) is itoa http://www.cplusplus.com/reference/clibrary/cstdlib/itoa.html

int(var) is done in C using atoi http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

Because strings in C are not handled as easily as in basic, you have to do a little more work to use them. I think you can do what you want with atoi but FWIW, I modified my LCD library by adding the following method to print numbers:

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

Hi fella, that did the trick :slight_smile:

and these are the batwings :slight_smile:

Steve

good to hear it works.

nice wheels!