I have an example piece of code and I want to print the variables to the log, specifically a variable named &results I wrote a line in the code at the end of void loop() to print the variable to the serial print out.
This isn't the full code but a large segment at least.
{
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (irrecv.decode(&results)) // have we received an IR signal? &results is
the variable
Serial.println(&results) //<-- My line of code
{
translateIR();
irrecv.resume(); // receive the next value
}
}/* --(end main loop )-- */
I expected the output to be the variable's content but it spat out no matching function for call to "println(decode_results*)" whilst compiling.
You can't simply use Arduino's print to print C structs. You can only print simple C data types like float, String, int, etc. So you need to print each field of your struct separately. I don't know decode_results but you can print its fields with something like this:
{
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
} /*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (irrecv.decode(&results)) // have we received an IR signal? &results is the variable
{
Serial.print("Results: ");
Serial.print(results.field1); // first field
Serial.print(" , ");
Serial.println(results.field2); // second field
}
translateIR();
irrecv.resume(); // receive the next value
} /* --(end main loop )-- */
Related
I am using FreeRTOS and would like to get the name of the task being executed and print it. How can I extract the name of the task being executed from the handler and print it. For example, I have the following task being executed. I tried printing out the name as follows -
void TaskBlink(void *pvParameters) // This is a task.
{
(void) pvParameters;
TaskHandle_t xHandle;
TaskStatus_t xTaskDetails;
// initialize digital LED_BUILTIN on pin 13 as an output.
pinMode(LED_BUILTIN, OUTPUT);
for (;;) // A Task shall never return or exit.
{
xHandle = xTaskGetHandle( "Task_Name" );
Serial.println(xHandle);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
}
}
But I get the error - Compilation error: no matching function for call to 'println(TaskControlBlock_t*&)'
Edit - Apprarently, I had to update the USE_TRACE_FACILITY flag to 1 in FreeRTOSConfig.h as per this link but I now get the the error -
Blink_AnalogRead/Blink_AnalogRead.ino:80: undefined reference to `xTaskGetHandle'
Why the compilation error
It seems that you have a little confusion about how the xTaskGetHandle works. Per documentation
TaskHandle_t xTaskGetHandle( const char *pcNameToQuery );
And returns:
If a task that has the name passed in pcNameToQuery can be located then the handle of the task is returned, otherwise NULL is returned.
Now, apparently you already have the name of the task (in the universal conception that the name is a sequence of characters) and you are using, so it's unclear what you want to get.
Anyway, the function returns a TaskHandle_t which is not a string, hence when you pass such type to the println function the compilation fails because it is expecting a string like object.
Get task name
In order to get the task name from the handler you need the function vTaskGetInfo and the struct TaskStatus_t .
Here's how you do it in your function:
void TaskBlink(void *pvParameters) // This is a task.
{
(void) pvParameters;
TaskHandle_t xHandle;
TaskStatus_t xTaskDetails;
// initialize digital LED_BUILTIN on pin 13 as an output.
pinMode(LED_BUILTIN, OUTPUT);
for (;;) // A Task shall never return or exit.
{
xHandle = xTaskGetHandle( "Task_Name" );
TaskStatus_t xTaskDetails;
/* Check the handle is not NULL. */
configASSERT( xHandle );
/* Use the handle to obtain further information about the task. */
vTaskGetInfo( /* The handle of the task being queried. */
xHandle,
/* The TaskStatus_t structure to complete with information
on xTask. */
&xTaskDetails,
/* Include the stack high water mark value in the
TaskStatus_t structure. */
pdTRUE,
/* Include the task state in the TaskStatus_t structure. */
eInvalid );
Serial.println(xTaskDetails.pcTaskName);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
}
}
My goal is to test individual servos for a project I'm doing.
Which may have faulty servos or bad wiring,
though the program I created for testing simply asks and returns an integer from input, then attaches a servo to it. But fails to return the correct value.
Code:
#include <Servo.h>
Servo s;
int pin;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Enter Pin");
}
void loop() {
// Getting Input
while (Serial.available() == 0) {}
pin = Serial.parseInt();
Serial.println(pin); // returns value given. <-- this is where it breaks and always returns "0".
//attaching servo
s.attach(pin);
// Testing Servo
s.write(90);
delay(1000);
s.write(0);
}
Your code works fine but it reads '\n' (EOL) character and parses it to 0.
Calling parseInt with SKIP_ALL and ignore '\n' helps to solve the issue:
pin = Serial.parseInt(SKIP_ALL, '\n');
currently, I am working on a project to read char inputs from serial monitor and command Arduino to switch on/off specific pins. The problem I am facing is, I am unable to read the complete char array entered in the serial monitor. can anyone tell me what am I doing wrong?
#define X 13 //led pin
char txt[15];
int i;
int Status=0;
void setup() { // put your setup code here, to run once:
pinMode(X,OUTPUT);// setting the pin flow of control as output
Serial.begin(9600);
while(!Serial)
{
; //to wait for pc to connect
}
Serial.println("\nHome Automation");
dashprint();
}
void loop() { // put your main code here, to run repeatedly:
if(Serial.available()>0)
{ i=0;
while(Serial.available()>0) //if serial available
{ char inchar=Serial.read();
txt[i]=inchar; // add char to txt string
i++;// increment to where to write next
txt[i]='\0'; //null termination
}
Serial.print(txt);
check();
}
}
void dashprint() //to print dashes
{
Serial.println("-----------------------------------------------");
Serial.println("give me some command"); //ask for command
}
void check()
{ if(strncmp(txt,"ON",2)==0)
{
digitalWrite(X,HIGH);
Status=1;
}
else if(strncmp(txt,"OFF",3)==0)
{ digitalWrite(X,LOW);
Status=0;
}
else if(txt=="STATUS")
{
}
else Serial.println("ERROR");
}
output:
Home Automation
give me some command
OERROR
NERROR
ERROR
expected output:
Home Automation
give me some command
ON
Your arduino is too fast to read the text "ON" in one round.
9600 is 1 ms per character.
A simple workaround is, to add a little delay
if(Serial.available()>0) {
delay(3); // wait for the whole message
i=0;
while(Serial.available()>0) {
...
ADD:
Additionally, you obviously receive a '\n' (newline) character. Make sure it's not causing troubles.
And increase the delay or have a better approach in general, if you expect more than 3 characters ( "STATUS" )
struct MYObject {char a[256];};
//structure works well with EEPROM put and get functions.
//also lets to input large strings, more then 64 bytes, as http
void setup() {
MYObject str ={" "};
Serial.begin(115200);
while (!Serial.available()) delay (10); //wait for string
int i = 0;
int k= Serial.available();
while (k > 0){
char inchar = Serial.read(); //read one by one character
str.a[i] = inchar;
i++;
if (k < 3) delay (10); //it gives possibility to collect more characters on stream
k = Serial.available();
}
str.a[i]='\0'; //null terminator
//now lets see result
Serial.println(str.a);
//.....
}
I am currently trying to get a value (Yes!) from the Arduino to a text file (data.txt).
The problem is that the data isn't being read from the Arduino's Serial. When I tried to simple print the value to the prompt in processing, I came out empty handed.
Below is my code for the Arduino
//Just a basic program to write to the Serial the word/phrase; `Yes!`.
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Yes!");
}
Below is my code for processing:
import processing.serial.*;
Serial mySerial;
PrintWriter output;
void setup() {
mySerial = new Serial( this, Serial.list()[0], 9600 );
output = createWriter( "data.txt" );
}
void draw() {
if (mySerial.available() > 0 ) {
String value = mySerial.readString();
if ( value != null ) {
output.println( value );
}
}
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
Yes, this code was found from this stackoverflow question.
Any help would be greatly appreciated!
Check out this line:
mySerial = new Serial( this, Serial.list()[0], 9600 );
This line assumes you only have one serial connection. If you have more than one serial connection, you're going to have to change the hard-coded index being used in this line.
I'm unable to store the serial.port value in a variable. I want to send a message from Android telnet app, on and off. If on comes I want to print fan on, if off comes I want to print off. I'm able to print on and off while I'm statically fixing value. I'm unable to store the stream in a variable.
String stringOne;
void setup() {
digitalWrite(13, LOW);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// send an intro:
Serial.println("\n\nString substring():");
Serial.println();
pinMode(13,OUTPUT);
}
void loop() {
digitalWrite(13,LOW);
// Set up a String:
stringOne ="+IPD 0,14 :ON";
int length = stringOne.length();
Serial.println(stringOne.length());
Serial.println(stringOne);
if (Serial.available() > 0) {
// substring(index) looks for the substring from the index position to the end:
if (stringOne.substring(length-2,length) == "ON") {
Serial.println("FAN ON");
digitalWrite(13,HIGH);
// delay(2000);
}
if (stringOne.substring(length-3,length) == "OFF") {
Serial.println("FAN OFF");
digitalWrite(13,LOW);
// delay(2000);
}
}
// you can also look for a substring in the middle of a string:
// do nothing while true:
while (true);
}
Well, while editing your question, I couldnt help noticing that infinite loop at the end of your code.
// do nothing while true:
while(true)
In this case, even if your code was all right, you cant expect to get next data.
void loop --> Remember it is itself a infinite loop
update 1:
your logic to use the serial port is wrong;
Remember, serial port only recieves a single character at a time.
if you send "hello" from pc, at the other end, arduino will recieve h, e, l, l, o
The trick is to collect all letters into a array. and then use our logic in it.
char commandbuffer[20]; //an array to hold our characters
int i=0;
if (Serial.available() > 0) {
while( Serial.available() >0) { //read until all data we send arrives
char c = Serial.read();
commandbuffer[i]= c; //we are actually storing it one by one
i++;
}
}
commandbuffer[i]='\n';
for(int j = 0; j<i; j++){
Serial.print(commandbuffer[j]);// and show it one by one too
}
now when you send "hello", it will print hello back. I hope this give you some idea. Happy coding.