I'm currently experimenting with sending a string to my Arduino Yun and trying to get it to reply back depending on what I send it.
I picked up a framework of some code here and have been experimenting with it but apart from the serial monitor displaying 'ready' I can't make it go any further.
The code is:
//declace a String to hold what we're inputting
String incomingString;
void setup() {
//initialise Serial communication on 9600 baud
Serial.begin(9600);
while(!Serial);
//delay(4000);
Serial.println("Ready!");
// The incoming String built up one byte at a time.
incomingString = "";
}
void loop () {
// Check if there's incoming serial data.
if (Serial.available() > 0) {
// Read a byte from the serial buffer.
char incomingByte = (char)Serial.read();
incomingString += incomingByte;
// Checks for null termination of the string.
if (incomingByte == '\0') {
// ...do something with String...
if(incomingString == "hello") {
Serial.println("Hello World!");
}
incomingString = "";
}
}
}
Can anyone point me in the right direction?
Thanks
I suspect the problem is that you're adding the null terminator onto the end of your string when you do: incomingString += incomingByte. When you're working with string objects (as opposed to raw char * strings) you don't need to do that. The object will take care of termination on its own.
The result is that your if condition is effectively doing this: if ("hello\0" == "hello") .... Obviously they're not equal, so the condition always fails.
I believe the solution is just to make sure you don't append the byte if it's null.
Try This:
String IncomingData = "";
String Temp = "";
char = var;
void setup()
{
Serial.begin(9600);
//you dont have to use it but if you want
// if(Serial)
{
Serial.println("Ready");
}
//or
while(!Serial)
{delay(5);}
Serial.println("Ready");
void loop()
{
while(Serial.available())
{
var = Serial.read();
Temp = String(var);
IncomingData+= Temp;
//or
IncomingData.concat(Temp);
// you can try
IncomindData += String(var);
}
Serial.println(IncomingData);
IncomingData = "";
}
Related
I want to get Processing to read Strings from Arduino.
I send two Strings massages from the arduino and I want to store them in two different variables on Processing.
I tried to do it, but the two Strings are passed to the first variable and the second variable remains empty. I don't understand why this is the case. Can someone help?
Regards
Arduino Code
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("1.first message");
Serial.println("2.second message");
delay(100);
}
void loop() {
}
Processing Code
import processing.serial.*;
Serial myPort;
void setup() {
myPort=new Serial(this, "COM3", 9600);
}
void draw() {
String s1=myPort.readStringUntil('\n');
String s2=myPort.readStringUntil('\n');
// printing variables
if(s1!=null){
print("s1:",s1);
}
if(s2!=null){
println("s2:",s2);
}
}
The following works on my Mac system. The incoming strings are placed in a string array as they arrive. The string at index[0] then becomes s1 and the string at index[1] is s2. I also added a delay(100); between the two strings on the Arduino side, but this may not be necessary; you can try it both ways.
import processing.serial.*;
Serial myPort;
String[] s; // Array to hold two strings.
int counter = 0;
void setup() {
printArray(Serial.list()); // List of serial ports
// Enter appropriate number for your system
myPort = new Serial(this, Serial.list()[2], 9600);
s = new String[2];
println("===========");
}
void draw() {
String str = myPort.readStringUntil('\n');
if(str != null) {
s[counter] = str;
if(counter == 0){
println("s1 = ",s[0]);
} else {
println("s2 = ",s[1]);
}
counter++;
}
}
I have 2 Arduino and 2 xbee. I send 2 sensor data from Arduino 1 (router) to Arduino to (coordinator):
On coordinator, I receive wireless data from this 2 sensors(from router) perfectly.
The data stream is something like this:
20.1324325452924 divided in: -first sensor(temperature): 20.1324325452 -second sensor(gas):924
My goal is to have these 2 values as 2 variables that get updated constantly and then pass these values on to the rest of the program to make something like print on LCD or something else:
temperature=20.1324325452 gas=924
I managed to divide that initial string that I receive on serial (20.1324325452924) in 2 variables but values from this 2 variables not updating like in the initial string (when sensor values are changed):
My code:
LiquidCrystal lcd(12,11,10,9,8,7);
String temperature;
String gas;
String readString;
char IncomingData[13];
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0)
{
char IncomingData = Serial.read();
readString += IncomingData ;
temperature = readString.substring(0, 13); //get the first 13 characters
gas = readString.substring(13, 16); //get the last 3 characters
Serial.print(IncomingData); //here I have my string: 20.1324325452924 which is updating properly when I have sensor values changes
// Process message when new line character is DatePrimite
if (IncomingData == '\n')
{
Serial.println(temperature);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.write("T:");
lcd.print(temperature);
delay(500);
temperature = ""; // Clear DatePrimite buffer
Serial.println(gaz);
lcd.begin(16, 2);
lcd.setCursor(0,1);
lcd.write("G:");
lcd.print(gas);
delay(500);
gaz = ""; // Clear DatePrimite buffer
}
}
}
Output from serial: 20.1324325452924
20.1324325452 924
First string it's updating when I receive new sensor data but the next 2 remains the same every time. I'm stuck for days I don't know to do this work. All I need to do is to divide the initial string which contains the data from 2 sensors in 2 variables that get updated constantly and then pass these values on to the rest of the program to make something like print on LCD.
Does anyone have any idea how to make this work?
Split the data after you receive the complete string.
void loop() {
while(!Serial.available()); // wait till data to be filled in serial buffer
String incommingStr = Serial.readStringUntil('\n'); // read the complete string
String temperature = incommingStr.substring(0, 13);
String gas = incommingStr.substring(13, 16);
Serial.print(incommingStr);
Serial.println(temperature);
Serial.println(gas);
lcd.setCursor(0,0);
lcd.print(temperature);
lcd.setCursor(0,1);
lcd.print(gas);
delay(500);
}
You only need to call lcd.begin() once. Calling it from the setup() function.
you have to modify the program like this: (do an action on readString in loop)
// Process message when new line character is DatePrimite
if (IncomingData == '\n')
{
Serial.println(temperature);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.write("T:");
lcd.print(temperature);
delay(500);
temperature = ""; // Clear DatePrimite buffer
Serial.println(gaz);
lcd.begin(16, 2);
lcd.setCursor(0,1);
lcd.write("G:");
lcd.print(gas);
delay(500);
gaz = ""; // Clear DatePrimite buffer
readString = ""; //clear either you concatenate at each loop!!*******
}
I am learning JUCE and I am writing a program that just reads the input from the audio card and plays it back. Obviously this is just for learning purposes. I am using the audio application template. This is the code inside the getNextAudioBlock() function:
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
if(true) // this is going to be replaced by checking the value of a button
{
const int channel = 0;
if(true) // this is going to be replaced too
{
const float* inBuffer = bufferToFill.buffer->getReadPointer(channel, bufferToFill.startSample);
float* outBuffer = bufferToFill.buffer->getWritePointer(channel, bufferToFill.startSample);
for(int sample = 0; sample < bufferToFill.numSamples; ++sample)
outBuffer[sample] = inBuffer[sample];
}
else
{
bufferToFill.buffer->clear(0, bufferToFill.startSample, bufferToFill.numSamples);
}
}
else
{
bufferToFill.buffer->clear(0, bufferToFill.startSample, bufferToFill.numSamples);
}
}
The code is really simple: the content from the input buffer is copied directly to the output buffer. However, I am not hearing anything. What am I doing wrong?
I am using arduino to develop a small IoT project. Till now everything is working fine.
The steps of the projects are:
Create an XML String from sensor reading
Send the XML as POST to the server.
The odd thing is that the first three trials work perfectly. However, after that, the string holding the XML becomes empty.
Below is a portion of the code:
void loop()
{
while (Serial3.available()>0)
{
char c = Serial3.read();
if (gps.encode(c)){
createXML();
delay(1000);
if(seconds_passed==10000 && !IsXMLEmpty)
{
XML+="\n</start>";
Serial.print(XML);
Serial.print(F("Send to serveR"));
seconds_passed=0;
IsXMLEmpty=true;
String temp ="";
temp = "XML="+XML;
char *param = new char[temp.length()+1];
temp.toCharArray(param,temp.length());
Serial.println(param);
if(!postPage(serverName,serverPort,pushPage,"XML=123")) Serial.print(F("Fail "));
else Serial.print(F("Pass "));
XML="";
XML="<start>\n\t";
}
}
}
// if(data==false){ Serial.println("No data"); }
}
I thing the problem is with the String object. It might be a memory issue. Any idea?
I am having an issue with comparing strings in my program. I receive in serial data and save it to a string:
void serialEvent() {
if(!stringComplete){
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
Serial.println("COMPLETE");
}
I then do a compare on the string that was stored from the serialEvent function:
void setCMD(String a){
if(a == "01*00"){
busACTIVE=0;
// clear the string:
inputString = "";
stringComplete = false;
}
else if(a.equals("01*01")){
busACTIVE=1;
// clear the string:
inputString = "";
stringComplete = false;
}
I have several else if statements and then a else statement at the end:
else{
Serial.println("Command not Found");
Serial.println(a);
// clear the string:
inputString = "";
stringComplete = false;
}
I tried both == operator and equals() and neither will compare properly. below is a serial output:
Serial Output
As you can see one of my comparison statements looks for 01*01 and that is also what you see in the serial output window but the if statement did not equate to true. Can anyone help figure out why this is not working. Thanks
Try to edit this:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
Serial.println("COMPLETE");
}
into this:
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
Serial.println("COMPLETE");
}
else
inputString += inChar;
The reason is that if you are comparing "01*00" with "01*00\n" and, of course, the comparison fails.
Anyway I'd avoid using variable-sized buffers. I definitely prefer using fixed-size buffers for performance reasons. And also because microcontrollers are... micro! Don't waste their scarce resources on mallocs and frees...