I have a very simple task.. Using the code below, I want it to send out a variable's value instead of just "Hello". I've tried to assign a char variable to it, but it won't let me. I get this error:
txVW.cpp: In function 'void loop()':
txVW:11: error: invalid conversion from 'char' to 'const char*'
Would someone be able to explain this to me and show me how to fix it? Thanks so much for your help!
#include <VirtualWire.h>
//signal is at digital 12 pin
void setup()
{
Serial.begin(9600);
vw_setup(2000); // Bits per sec
}
void loop()
{
const char *msg = "hello";
Serial.println(msg);
vw_send((uint8_t *)msg, strlen(msg));
delay(400);
}
Sorry for the confusion.. The code I originally posted compiles, no problem. But when I edit it to this, the error pops up. Basically, I don't want it to print from a text directly. I want it to print from a variable so that later, I can just put in new values into the variable and see it print the whatever the variable is assigned to..
#include <VirtualWire.h>
//signal is at digital 12 pin
void setup()
{
Serial.begin(9600);
vw_setup(2000); // Bits per sec
}
void loop()
{
char note = "Hi!";
const char *msg = note;
Serial.println(msg);
vw_send((uint8_t *)msg, strlen(msg));
delay(400);
}
Everything in memory has an address. Arrays are in consecutive memory addresses. A pointer is a variable that contains an address. The pointer can be used to fetch the data at the address.
Rather than copy ann array of characters or a character string we usually pass around a pointer to the first character:
char *myString = "hello";
You can reference a pointer with the '*' operator OR you can use an array index as if it pointed to an array:
char firstChar = *myString;
OR
char firstChar = myString[0];
The note variable is defined as a pointer, but it does not point to any space where characters can be stored. Trying to then store characters there will overwrite something that should not be overwritten.
The note variable is defined as a pointer, but it does not point to any space where characters can be stored. Trying to then store characters there will overwrite something that should not be overwritten.
Yes, it will work. char * note; note="Hi!"; will declare a character pointer and then point it at the character string "Hi!". It is functionally equivalent to char * note = "Hi!";
Note that you can't COPY the string into the address pointed to by 'note' since 'note' doesn't point to anything when it is created:
char *note;
strcpy(note, "Hi!"); // This will fail, if you or lucky, or just corrupt memory
So below is my new code. I'm taking in the serial input and assigning it to memory allocation *char.. But it doesn't work.. I thought that the pointer will start on the nth slot, then go to the next based on the nth byte from serial?
Thanks again in advance!
#include <VirtualWire.h>
//signal is at digital 12 pin
void setup()
{
Serial.begin(9600);
vw_setup(2000); // Bits per sec
}
void loop()
{
if (Serial.available() > 0)
{
const char *incomingByte = Serial.read(); // assign serial input to a memory pointer?
Serial.println(*incomingByte);
vw_send((uint8_t *)incomingByte, strlen(incomingByte));
//delay(400);
}
}
do Serial.println(*incomingByte); works? if not because the pointer i spointing to the first buffer address, and the read has eliminated the value.
strlen(incomingByte) won't work: first of all read return only ONE charater, so size should be always 1: BUT you don't have put the final '\0', so the strlen get crazy.
should I just create an array, and build a for loop to start assigning each character to the array? I kinda wanted to learn how to do it using pointers though.. Anyone can shed light on this? Thanks!
const char *incomingByte = Serial.read(); // assign serial input to a memory pointer?
Serial.read() returns an int. Assigning the value to a pointer variable makes no sense.
You need a static char array and an index variable, to keep track of where to store the next character. Don't forget the terminating NULL, after each addition to the array.