Problems with atoi()

I’m using the following code which basically echoes onto terminal whatever I send to the arduino. However, atoi only works to convert the ascii code to integer, so i guess I shouldn’t have been surprised that the terminal echoed 0 for all non numeric characters. Is there someway to fix this?

#include <stdlib.h>
void setup(){
//Spi.mode((0<<CPOL) | (1 << CPHA)); // set SPI mode 1
Serial.begin(9600);
Serial.print(25, DEC);
delay(200);
Serial.print(17, DEC);
delay(5);

}
unsigned long time;
int command=0;
int array[20];
     int num=0;
     char* val="0";
void loop(){
    


    while(Serial.available()>0){
 
      val[0]=Serial.read();
    array[num]=atoi(val);
        if(array[num]!=int('

)){  
          Serial.print(array[num]);
     delay(50);
       num++;
      //  array[num]=’\0’;
       }else{
           
        for(int i=0; i<num; i++){  
   
        Serial.print(array[i]);
        delay(50);
        }
         while(num>=0){      //clears array
             array[num]=’ ';
             num–;
         }
      num=0;
       
       }
   }
 
 
}

Only call atoi if

if (val [0] >= '0' && val [0] <= '9')

Thanks, but is there some way to convert the non numeric characters too from ascii to string?

Your question doesn't really make sense. The non-numeric ("ascii") characters are already a string. What is there to convert?

sorry about the terminology. Right now, serial.read gets the ascii chars and the arduino outputs it on the terminal as decimal values. I want it to output exactly the same chars that I inputed.

So…“Serial.print(Serial.read())” ???

Here’s what I have changed so far. Now I got it working that the terminal outputs exactly the same nonumeric chars I input. But now, any numbers i enter gets outputted as “.”

#include <stdlib.h>
void setup(){
//Spi.mode((0<<CPOL) | (1 << CPHA)); // set SPI mode 1
Serial.begin(9600);
Serial.print(25, DEC);
delay(200);
Serial.print(17, DEC);
delay(5);

}
unsigned long time;
int command=0;
int array[20];
     int num=0;
     char* val="0";
void loop(){
    


    while(Serial.available()>0){
 
      val[0]=Serial.read();
      if (val [0] >= '0' && val [0] <= '9'){
    array[num]=atoi(val);
      }else{
        array[num]=val[0];
      }
        if(array[num]!=int('

)){  
          Serial.print(array[num]);
     delay(50);
       num++;
      //  array[num]=’\0’;
       }else{
           
        for(int i=0; i<num; i++){  
   if (array[i] >= ‘0’ && array[i]<= ‘9’){
        Serial.print(array[i], DEC);
        delay(50);
   }else{
   Serial.print(array[i], BYTE);
   delay(50);
   }
        }
         while(num>=0){      //clears array
             array[num]=’ ';
             num–;
         }
      num=0;
       
       }
   }
 
 
}

nvm, i got it working, except that it take a considerable time for a response after i type something on the terminal for something to return. is there anything that I'm doing that's making it so slow?

is there anything that I’m doing that’s making it so slow?

All those "delay"s?

But now, any numbers i enter gets outputted as “.”

because:

array[num]=atoi(val);

I think a lot of your problems are caused by the word "DEC". You may want to check out http://arduino.cc/en/Serial/Print

Good luck!