Converting string or int to const char

Hi, I am very new at programming,
I am trying to read the analog input of a pot (0-255) and send it over an rf transmitter with virtualwire to another arduino receiver.
However I dont know how to convert the integer value i am getting from the analogRead input to the const char required.
I have converted the integer to a string, however I am thinking that probably isnt necessary and that you can probably convert the int directly to a char.
I have heard i need to use atoi or something to convert but I dont know how to do that.

here is my code:

#include <VirtualWire.h>

const int led_pin = 11;
const int transmit_pin = 8;
const int receive_pin = 2;
const int transmit_en_pin = 3;

void setup()
{
  // Initialise the IO and ISR
  vw_set_tx_pin(transmit_pin);
  vw_set_rx_pin(receive_pin);
  vw_set_ptt_pin(transmit_en_pin);
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);	 // Bits per sec
  Serial.begin(9600);
}

byte count = 1;

void loop()
{
  analogRead(A0);
  int i = map(i,0,660,0, 255); //gets values from 0-255 from pot
  
  String f = String(i); //converts int to string
  char *c = ("i"); //<---------here is my error 
  
  Serial.println(i); //shows the value of the pot on computer
  

  
  analogWrite(led_pin, 255); // Flash a light to show transmitting
  vw_send((uint8_t *)c, 5); //send data
  vw_wait_tx(); // Wait until the whole message is gone
  digitalWrite(led_pin, LOW); 
  delay(500);
  
  
}

thanks for any help! please remember I am very very new to code when you respond!

You are mapping your reading onto a range from 0 to 255, so I assume that you want to store your value into a char, or byte, that can hold values between 0 and 255 (not a char with values between '0' and '9'). If so, you may just assign your variable of type int to a new variable of type byte. A safer way is to strip off any extra bits using bitwise AND.

byte b;
// your code
b = i & 0xff;

Ditch the String class and use itoa().

Arrch: Ditch the String class and use itoa().

yeah but my question is how to use itoa()

spatula: You are mapping your reading onto a range from 0 to 255, so I assume that you want to store your value into a char, or byte, that can hold values between 0 and 255 (not a char with values between '0' and '9'). If so, you may just assign your variable of type int to a new variable of type byte. A safer way is to strip off any extra bits using bitwise AND.

byte b;
// your code
b = i & 0xff;

I dont understand this at all, could you explain?

plasmaninjaa: I dont understand this at all, could you explain?

Let's say your int variable is = 5. You can use itoa() to convert it into a string = "5", then take the first character of the string and get '5'. Same if your int is = 9. But if your int is 10, then itoa() returns "10" and you cannot convert it into a single char.

Your code:

int i = map(i,0,660,0, 255); //gets values from 0-255 from pot

tells me that i can take values from 0 to 255. So you cannot use itoa().

But an integer between 0 and 255, as in your case, is a byte, so it can be put into a variable which is just one byte long. I use the 'byte' type instead of the 'char' type just to make clear that it should not be treated as a character (you cannot print it), but internally byte and char are the same.

So you can just write: b = i. Really. The other form b = i & 0xff is only more explicit in indicating that you are dropping off any extra bits. So if you change your scale you can remember to change the size of your variable. It's more for readability than for function (in this case at least). The & operator is a bitwise AND operator, you'll probably see it used a lot (with the bitwise OR, which is |) in programs that need to read from sensors and set or get the 'high bit', 'low bit' or things like that. It's a basic operation in C and there's a lot of documentation around.

An example of using itoa():

int i = 25;
char buf[4];
// first argument to itoa() is the number to convert,
// second argument the buffer (must already be allocated), 
// third argument the base (2=binary, 10=decimal, 16=hex, ...)
itoa (i, buf, 10);
// buf = "25"

Btw, your are not converting to const char, since it is obviously a variable that may change.

[edit]Uh, I finally got it. What you wanted is a const char * instead of a string. You may safely ignore my comment and explanation.

plasmaninjaa: yeah but my question is how to use itoa()

Plenty of examples on Google.

Why do you need to convert a byte sized value to a collection of byte sized values (a string) to send? Then, you need to convert the collection of byte sized values back to a byte in order to use it.

  analogRead(A0);
  int i = map(i,0,660,0, 255); //gets values from 0-255 from pot

Read and discard the potentiometer value. Then map the value in a global variable to a byte sized value, and store in an int.

What a load of rubbish.

Start with getting the basics, like scope and calling functions that return values, right before you try complex stuff like wireless transmission.

hello please i neeed urgent help
my code is attached to this message and this is the error error

Arduino: 1.6.6 (Windows 10), Board: “Arduino Pro or Pro Mini, ATmega328 (5V, 16 MHz)”

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino: In function ‘void setup()’:

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:28:160: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“07060471648”, “GSM BASED SMARTHOME SECURTTY SYSTEM DESIGNED BY: YUSUFF JOSHUA (12/ENGO4/064). SUPERVISED BY: MR. FOLORUNSHO. SYSTEM IS READY!!!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:28:160: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:30:168: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“08030829362”, “GSM BASED HOME SECURTTY SYSTEM DESIGNED BY: OGUCHE ATTAH CHRISTOPHER (HEET/T/330). SUPERVISED BY: ENGR. B. A. KWEMBE. SYSTEM IS READY!!!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:30:168: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino: In function ‘void loop()’:

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:38:90: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“07060471648”, “EMERGENCY!!! There is an intruder in Your Building, Sir!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:38:90: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:40:90: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“08030829362”, “EMERGENCY!!! There is an intruder in Your Building, Sir!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:40:90: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:45:92: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“07060471648”, “EMERGENCY!!! There is Fire outbreak in Your Building, Sir!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:45:92: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:47:92: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“08030829362”, “EMERGENCY!!! There is Fire outbreak in Your Building, Sir!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:47:92: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:52:93: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“07060471648”, “EMERGENCY!!! There is Smoke incident in Your Building, Sir!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:52:93: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:54:93: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

sms.SendSMS(“08030829362”, “EMERGENCY!!! There is Smoke incident in Your Building, Sir!”);

^

C:\Users\Big Jeo\Desktop\PROJECT\raw-1\smarthome_security_system\smarthome_security_system.ino:54:93: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\GSM.cpp: In member function ‘virtual int GSM::begin(long int)’:

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\GSM.cpp:116:24: warning: overflow in implicit constant conversion [-Woverflow]

_cell.begin(115200);

^

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\GSM.cpp:177:21: warning: overflow in implicit constant conversion [-Woverflow]

_cell.begin(115200);

^

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp: In constructor ‘HWSerial::HWSerial()’:

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp:4:4: error: ‘Serial1’ was not declared in this scope

Serial1.begin(9600);

^

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp: In member function ‘void HWSerial::end()’:

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp:13:3: error: ‘Serial1’ was not declared in this scope

Serial1.end();

^

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp: In member function ‘virtual void HWSerial::begin(int)’:

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp:25:3: error: ‘Serial1’ was not declared in this scope

Serial1.begin(baud_rate);

^

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp: In member function ‘int HWSerial::peek()’:

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp:29:10: error: ‘Serial1’ was not declared in this scope

return Serial1.peek();

^

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp: In member function ‘virtual size_t HWSerial::write(uint8_t)’:

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp:33:10: error: ‘Serial1’ was not declared in this scope

return Serial1.write(byte);

^

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp: In member function ‘virtual int HWSerial::read()’:

C:\Users\Big Jeo\Documents\Arduino\libraries\SIM900IDE100MEGA\HWSerial.cpp:58:37: error: ‘p’ was not declar
ed in this scope

unsigned char c = pgm_read_byte(p++);

^

exit status 1
Error compiling.

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

smarthome_security_system.ino (4.38 KB)

It was NOT necessary to post all the warnings.

All of the errors are from the SIM900IDE100MEGA library that you did not post a link to.

I'd have to guess, though, based on the name, that that library is NOT for use on a Mini or Pro.