Anyone know a good way to go about this? I was thinking you could write some code that essentially takes a char array and adds it piece by piece to a blank string?
I found string.CharArray but it doesn't seem to be working for me at least...?
To make a string (note small s) from an array of characters just add a NULL ('\0') to the end. If you can show some code that shows where the character array is coming from or how it is stored we can show how to add the NULL.
If you are actually talking about Strings (big S, String objects), that is a different story. You are better off not using String objects in the Arduino with its limited memory.
Can you explain the difference between a string and a String?...
#include <SPI.h>
#include <LiquidCrystal.h>
const int slaveSelectPin = 10;
int read1 = 0;
int read2 = 0;
int num = 0;
String alt = "";
char serin1[40] = {' '};
char pup[40];
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(20, 4);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode (slaveSelectPin, OUTPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE3);
Serial.begin(9600);
}
void loop() {
read1 = digitalRead(8);
read2 = digitalRead(9);
String alt = String(num);
String.toCharArray(pup, 40);
// when characters arrive over the serial port...
// wait a bit for the entire message to arrive
delay(300);
// clear the screen
// read all the available characters
if(read1 == HIGH){
lcd.clear();
num = num + 1;
lcd.print(alt);
int availableBytes = 0;
boolean change = false;
int i = 0;
while (availableBytes == 0)
{
availableBytes = Serial.available();
delay(75); //this gives the buffer time to fill
}
availableBytes = Serial.available();
for(i=0; i<availableBytes; i++)
{
if (i < 40)
{
serin1[i] = alt;
}
else
{
Serial.read();
}
change = true;
}
if (change == true)
{
for(i=availableBytes; i < 41; i++)
{
serin1[i] = ' ';
}
COM(0x01); //clear all display and set DD-RAM address 0 in address counter
COM(0x02); //move cursor to the original position
COM(0x06); //set the cursor direction increment and cursor shift enabled
COM(0x38); //set 8bit operation,2 line display and 100% brightness level
COM(0x80); //set cursor to the first position of 1st line
COM(0x0c); //set display on,cursor on,blinking off
COM(0x80);
for (i=0; i<20; i++)
{
DATA(serin1[i]);
}
COM(0xc0);
for (i=20; i<40; i++)
{
DATA(serin1[i]);
}
}
}
delay(300);
}
void counter(){
}
void DATA(unsigned char temp_1)
{
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(0xfa);
SPI.transfer(temp_1);
digitalWrite(slaveSelectPin,HIGH);
}
void COM(unsigned char temp_2)
{
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(0xf8);
SPI.transfer(temp_2);
digitalWrite(slaveSelectPin,HIGH);
}
So... that is the entirety of the code... and it's probably horrendous. I a trying to work with two displays, one a VFD and the other an LCD. I want to have "communication" of sorts between them with a single piece of data. However the VFD requires a char array while the LCD needs a string (I think, could be totally wrong here), so that is why I am trying to convert a char array to a string. I want to swap the Serial.read out with the string or data that I want to be put up on the screen.
A string (lower case s) is just an array of characters terminated with a '\0' (null character); it has a pre-defined size (e.g. char text[21];
). A String (capital S) is a class and you instantiate an object of it. It can grow (using e.g. concat). But it can leave holes in your memory where you can hide an elephant; unpredictable behaviour of your code at run time can be the result.
Correct me if I am wrong, but could I just use a string, with a set size, and easily translate that into a character array to fix this?...
A string IS a char array. You only need a NULL terminator after the last valid char in that array
soo is calling it a string just acting as a name for the character array? :
If you look at it that way, so is char itself.
A string is just a null terminated array of chars