Arduino Serial library cannot find "availableForWrite()" function - c++

I'm trying to get Arduino to send some chars back to my raspberry pi via the serial USB port.
However, when I try to use the function "Serial.availableForWrite()", the compiler failed with:
error: ‘class HardwareSerial’ has no member named ‘availableForWrite’
the rest of the code piece works fine though.
Here is the entire code:
void setup() {
// connect to serial
Serial.begin(9600);
}
void loop() {
// write value
if (Serial.availableForWrite() > 0) {
Serial.write("0.587");
}
Serial.write("Error");
delay(1000);
}
I am pretty sure my Arduino IDE is up to date, and also I don't think I have spelling mistakes. What could be causing this problem?

i dont have any problems with this code neither when compiling and i can even upload this sketch to my arduino uno this problem might be ocuring due to the usage of an raspbery pei maybe try and use an arduino board

Related

I can't see the output from Serial.print. Why?

I have been trying to setup a program analyzing stock-prices for quite some time. I didn't manage to get any output however, so I decided to do some testing with simpler programs... MUCH simple programs. Right now I can't even manage to get "Hello World" to work, using platformio. If someone could tell my why I would appreciate it a lot.
main.cpp:
#include <Arduino.h>
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.print("Hello World!");
}
platformio.ini:
[env:esp32-poe-iso]
platform = espressif32
board = esp32-poe-iso
framework = arduino
monitor_speed = 115200
I have tried changing the baud-rate and setting different COM-ports as the monitor-port. I don't manage to get any good result / any result at all however, and I am running out of ideas to why

Raspberry Pi Pico crashes when using SPI1 but not when using SPI0

so I am having a weird issue, I am using the C/C++ SDK with visual studio to program a raspberry pi pico. The project is using a rotary encoder with push button and lcd to control a seperate PCB. Almost everything is working but when I try and send an SPI command on SPI1 the program just completely stops working, as in using the encoder or button causes no change in either serial monitor or in the lcd.
I know it is an issue with the spi1 because if I change it to spi0 I get no crash and I also tried changing the value I was sending on spi1 and that made no difference and it still crashed.
Below is a part of the code that shows how I used spi0 which works and spi1 which doesnt work, I can include the full project if needed but it is kinda long so, let me know if it is needed.
if(Mode_Counter == 1) { // data to send on SPI for Mode 1
Ctrl_bit_and_data = data.Ctrl_bit_and_Data_Func_Write();
spi_write16_blocking(SPI_PORT, &Ctrl_bit_and_data, 1); // this is spi0 and works fine
}
if(Mode_Counter == 3) {
Counter_LV = Counter;
Count_LV.set_position_LV(Counter_LV);
Ctrl_bit_and_data_LV = data_LV.Ctrl_bit_and_Data_Func_Write_LV();
spi_write16_blocking(spi1, &Ctrl_bit_and_data_LV, 1); // this is the line that isnt working
}
Any help or guidance with this would be greatly appreciated I am very stuck as to the issue with this.

Keyboard not found even though I'm including Keyboard.h

I want to make a simple program to put on my Arduino/Genuino (Sunfounder) Uno board that when plugged in, it invokes the Keyboard.print() function. When I compile my program to do that, it says that I'm not including Keyboard.h even though I actually am at the beginning of my program.
My code:
#include <Keyboard.h>
void setup() {
Keyboard.begin();
Keyboard.print("Hello, world!");
Keyboard.end();
}
void loop() {
}
When I compile the code, I get this error:
KeyboardMessage:4:3: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
I checked my libraries file in the Arduino sketch editor file and Keyboard.h is there.
Any help is appreciated.
Arduino/Genuino Uno does not support Keyboard. You can see it in hardware/ardunino/avr/libraries/HID/HID.h. This file is included from Keyboard.h and contains #if defined(USBCON). But USBCON is not defined for Arduino/Genuino Uno. You need to use another board for Keyboard.h
Following controllers support it:
ATmega32U6
ATmega8U2
ATmega16U2
ATmega16U4
ATmega32U2
ATmega32U4
and some ATxxUSBxx
Arduino/Genuino Uno uses ATmega32U8.
You know what you don't require the 32u4 now, I got a solution to use keyboard and mouse through any Arduino.
All you have to do is something like this:
Serial.println("pressA")
On the Arduino
Head to a python script and do this:
import serial
import pyautogui
Arduino_Serial = serial.Serial('COM5', 9600)
while 1:
incoming_data = str(Arduino_Serial.readline())
print(incoming_data)
if 'pressA' in incoming_data:
pyautogui.press('a')
incoming_data = ""

C++ serial communication in Linux

I am trying to send data from my BeagleBone black board to Arduino Uno. The baud rate I have selected is 300. I am using the serialib library which is located here: http://serialib.free.fr/html/classserialib.html#ac8988727fef8e5d86c9eced17876f609 you can scroll all the way to the bottom to view the two files (serialib.h and serialib.cpp), however I have posted the main snippets here too. I read some reviews saying that this library is not reliable however I would first want to check my code before really suspecting anything else.
This is the program I have written in C++ on my BeagleBone:
#include <iostream>
#include "serialib.h"
#ifdef __linux__
#define DEVICE_PORT "/dev/ttyO1"
#endif
using namespace std;
int main()
{
serialib LS; //the main class to access
int Ret;
Ret= LS.Open(DEVICE_PORT,300);
if (Ret!=1)
{
cout<<"cant open port\n";
return 0;
}
else{cout<<"port now open \n";}
string xval="650X450Y";
for(int i=0;i<500;i++)//send xval 500 times
{
for(int j=0;j<xval.length();j++)//send each character separately
{
Ret=LS.WriteChar(xval[j]);
LS.Close();
LS.Open(DEVICE_PORT,300);
}
if (Ret!=1){cout<<"cannot write\n";}
else{cout<<"done writing\n";}
}
LS.Close();
cout<<"Transmission complete\n";
}
My code on the Arduino Uno is as follows:
#include <SoftwareSerial.h>
SoftwareSerial uart(10,11);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //baud rate for Serial communication
uart.begin(300);
}
char x;
String data="";
void loop()
{
if(uart.available()>0)//check if data is coming in
{
while(uart.available())
{
x=uart.read();//read the incoming byte
data+=x;//append the string with incoming bytes
}
if(x=='Y')//received all the bits
{
Serial.println(data); //display received information
}
}
}
C++ - I am first opening up my UART port on the BeagleBone and sending the string "650X450Y" character by character which is repeated 500 times to see if my communication system is robust or not. As you can see that within the 'for' loop I am closing and opening this port after sending every character because without this, it sends quite a lot of wrong data and if this 'for' loop is very big then the writing process even stops (not sure why it behaves like that) thus after closing and opening this port every time, I have managed to reduce the errors significantly but there are still a few errors:
I sent this string 500 times but one sample I received on the Uno was "660X450Y", wrong value.
About 2-3 times out of 500, I am receiving "650X450Y650X450Y650X450Y" i.e. repetitions, this is definitely not the string length then how come it can send this data?.
The rest of the strings I received on the Uno are perfect.
On the Uno as you can see that I am reading in character by character and appending it to my string named data and printing out this data as soon as the byte 'Y' is detected which denotes the end of the string. I previously used the WriteString() function in my C++ code however that gave a number of errors, The code I have provided is the closest I have come so far in the last few days to make this system 100% reliable and robust after lots of debugging, however I'm really not sure why the system is still not 100%.
I saw the source code of both the files in the library and observed the WriteChar(char Byte) function which is defined at line 210 in serialib.cpp (link already provided above) and I see that this is the function transmitting the characters:
if (write(fd,&Byte,1)!=1) // Write the char
return -1; // Error while writting
return 1; // Write operation successfull
I don't see anything wrong with this function then why can't I receive the data with 100% accuracy, Is there anything wrong in both my source codes or either one?, should I opt for a different serial library?, in case I opt for other libraries and I still don't get my results then I think I may have to transmit this info in a wireless manner for e.g using bluetooth modules. If anyone has any suggestions/improvements regarding this problem then do let me know :), till then I'll try other methods to achieve a 100% accuracy.
The easiest just to check if quickly can be done in the linux terminal and no library is used for that.
Set the baudrate to 300 for the UART1 (stty -F /dev/ttyO1 raw & stty -F /dev/ttyO1 300)
You can sent data to the UART1 simply by writing(or reading) to the device file for the UART1 which is in your case /dev/ttyO1
Writing: echo "650X450Y" > /dev/ttyO1
Reading: cat /dev/ttyO1
If you want to do this in C Code you can use the open/read/write/close syscall functions.

GPIO Pins RaspberryPi Using C++

I'm writing a program to control the GPIO pins on my raspberryPi with C++ and having difficulty I'm able to export with the following code:
char pathString[256];
sprintf(pathString, "%s/export", "/sys/class/gpio");
ofstream exporterFile(pathString);
exporterFile << pinNumber;
exporterFile.close()
This works an successfully exports the pin but this does not set the direction:
sprintf(pathString, "%sgpio%d/direction", "/sys/class/gpio", pinNumber);
ofstream directionFile(pathString);
directionFile << pinDirection;
directionFile.close();
For some reason I cannot write to the file, perhaps I do not have the right privileges. My question is, is that the problem and if so how do I solve it so I can write to the file.
Thanks in advance
You need to be root or run your program with sudo in order to use the GPIO pins.
However, I'd recommend using the wiringpi library http://wiringpi.com/ to access GPIO from c/c++. It is easy to use and raises the abstraction level a bit. It also lets you do things like PWM. A program using wiringpi also needs to be run with sudo.