Char Variable Question

I am using a char variable within a series of if statements. If I set the char variable equal to ‘ABC’ and serial print the value, it prints only ‘C’. This is consistent with all other char variables also, in that only the last character is printed. Is there a way to set a string of characters into a variable?

Thanks!

And the code:

volatile byte count;

unsigned int duration;

unsigned long timePrevious;

float windSpeed = 0;

int windVein = 0; 

int windReading = 0;

char windDirection = 'null';



void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, arduino_anemometer, RISING);

  count = 0;
  duration = 0;
  timePrevious = 0;
  windReading = 0;
}

void loop()



{
  if  (windReading > 55 && windReading < 70) {
    windDirection = ('SSE'); 
  }  

  if  (windReading > 75 && windReading < 89) {
    windDirection = ('NEE'); 
  }  

  if  (windReading > 90 && windReading < 110) {
    windDirection = ('E'); 
  }  

  if  (windReading > 119 && windReading < 140) {
    windDirection = ('SSE'); 
  }  

  if  (windReading > 160 && windReading < 199) {
    windDirection = ('SE'); 
  }  

  if  (windReading > 225 && windReading < 265) {
    windDirection = ('SSW'); 
  }  

  if  (windReading > 270 && windReading < 315) {
    windDirection = ('S'); 
  }  

  if  (windReading > 385 && windReading < 425) {
    windDirection = ('NNE'); 
  }  

  if  (windReading > 440 && windReading < 490) {
    windDirection = ('NE'); 
  }

  if  (windReading > 565 && windReading < 615) {
    windDirection = ('SWW'); 
  }

  if  (windReading > 616 && windReading < 650) {
    windDirection = ('SW'); 
  }

  if  (windReading > 670 && windReading < 720) {
    windDirection = ('NNW'); 
  }

  if  (windReading > 750 && windReading < 805) {
    windDirection = ('N'); 
  }

  if  (windReading > 815 && windReading < 890) {
    windDirection = ('NWW'); 
  }

  if  (windReading > 900 && windReading < 930) {
    windDirection = ('NW'); 
  }

  if  (windReading > 931 && windReading < 970) {
    windDirection = ('W'); 
  }

  windReading = (analogRead(0));

  if (count >= 1) { 
    duration = (millis() - timePrevious);
    timePrevious = millis();
    count = 0;
    windSpeed = (1492/duration);
    Serial.print(windSpeed,DEC);
    Serial.print(" MPH");
    Serial.print(",");
    Serial.print(windReading);
    Serial.print(",");
    Serial.println(windDirection);
  }
}

void arduino_anemometer()
{
  count++;
}

A char variable will store exactly what it says, ONE char, what you need is a string variable which in C (++) is just an array of chars.

Not to be too picky, but a string is a NULL-terminated array of chars.

Ahh yes.

That is so true.

Hi, unfortunately, the IDE hides compiler warnings (-w option). turning on the warnings (-Wall option) reveals:

/usr/bin/avr-g++ -c -g -Os -Wall    -fno-exceptions -ffunction-sections -fdata-sections -DF_CPU=16000000L -DARDUINO=18 -I/home/rick/arduino-0018/hardware/arduino/cores/arduino -mmcu=atmega328p applet/windspeed.cpp -o applet/windspeed.o 
applet/windspeed.cpp:16:22: warning: character constant too long for its type
applet/windspeed.cpp:37:22: warning: character constant too long for its type
applet/windspeed.cpp:41:22: warning: character constant too long for its type
applet/windspeed.cpp:49:22: warning: character constant too long for its type
applet/windspeed.cpp:53:22: warning: multi-character character constant
...etc...
applet/windspeed.cpp:16: warning: overflow in implicit constant conversion
applet/windspeed.cpp: In function 'void loop()':
applet/windspeed.cpp:37: warning: overflow in implicit constant conversion
...etc...

in a classic C program a string is implemented as: char charArrayName[] = "My string";

I'm sure you will have it all working within a few minutes.

Rick.

I'm sure you will have it all working within a few minutes.

If you are trying to declare and initialize a string,

char myArray[] = "My value";

is correct. If you then want to change the value in myArray, you need to use strcpy.

strcpy(myArray, "NewVal");

Care needs to be taken to assure that strcpy does not try to copy more data into the destination array than it can hold.

applet/windspeed.cpp:16:22: warning: character constant too long for its type

That's because:

int a = 'ab';
long b = 'abcd';

are perfectly legal.