Move linear actuator using arduino r4 wifi

Hi all! I'm working on a code that moves a linear actuator up and down based on the input given by a joystick app. This is my first code for an arduino, but I'm familiar with java so I'm not really sure what went wrong syntax wise and I'm not really sure how to understand the errors.

This is the code:

#include<Arduino.h>
#include<BluetoothSerial.h>

//Create BluetoothSerial object to communicate with app (Bluetooth module)
BluetoothSerial SerialBT;

const int relayPin=3; //Digital pin 3 connected to IN3 of the relay
const int powerPin=4; //Digital pin 4 (used as 5V power pin)
String command;

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin, OUTPUT); //set relayPin as output
  pinMode(powerPin, OUTPUT); //set powerpin as output
  Serial.begin(9600); //Baud rate for serial communication
  SerialBT.begin("MyActuator");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (SerialBT.available() > 0){
    String command=SerialBT.readStringUntil('/n');
    command.trim();

    if (command=="R"||command=="U"){
      digitalWrite(relayPin, HIGH);
      delay(1000);
      digitalWrite(relayPin, LOW);
    }
    else if(command=="D"||command=="L"){
      digitalWrite(relayPin, LOW);
      delay(2000);
      digitalWrite(relayPin, HIGH);
    }
    else if(command=="S"){
      digitalWrite(relayPin, LOW);
    }
    else{
      Serial.println("Invalid comment");
    }
  }
}

This is the error:

Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:7:17: error: no matching function for call to 'BluetoothSerial::BluetoothSerial()'
 BluetoothSerial SerialBT;
                 ^~~~~~~~
In file included from /Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:4:0:
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:42:5: note: candidate: BluetoothSerial::BluetoothSerial(arduino::HardwareSerial&, bool)
     BluetoothSerial(HardwareSerial& serial, bool verbose = true);
     ^~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:42:5: note:   candidate expects 2 arguments, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:17:7: note: candidate: constexpr BluetoothSerial::BluetoothSerial(const BluetoothSerial&)
 class BluetoothSerial {
       ^~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:17:7: note:   candidate expects 1 argument, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:17:7: note: candidate: constexpr BluetoothSerial::BluetoothSerial(BluetoothSerial&&)
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:17:7: note:   candidate expects 1 argument, 0 provided
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino: In function 'void setup()':
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:21:30: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
   SerialBT.begin("MyActuator");
                              ^
In file included from /Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:4:0:
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:51:10: note:   initializing argument 1 of 'void BluetoothSerial::begin(int)'
     void begin(int baudRate = 9600);
          ^~~~~
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino: In function 'void loop()':
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:35:16: error: 'class BluetoothSerial' has no member named 'available'
   if (SerialBT.available() > 0){
                ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:36:49: error: no matching function for call to 'BluetoothSerial::readStringUntil(int)'
     String command=SerialBT.readStringUntil('/n');
                                                 ^
In file included from /Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:4:0:
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:33:12: note: candidate: arduino::String BluetoothSerial::readStringUntil(char, int)
     String readStringUntil(char terminator, int timeout);
            ^~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/BluetoothSerial/src/BluetoothSerial.h:33:12: note:   candidate expects 2 arguments, 1 provided
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:53:16: error: 'class BluetoothSerial' has no member named 'println'
       SerialBT.println("Invalid comment");
                ^~~~~~~

exit status 1

Compilation error: no matching function for call to 'BluetoothSerial::BluetoothSerial()'

Thank you for the help!

Hi @lilly45. Did you find this code somewhere on the Internet? If so, please provide a link to the page where you found it in a reply here on the forum topic. That will give the forum helpers some valuable context that will allow us to more effectively assist you.

The "BluetoothSerial" library you installed has a different programming difference than what is used in your sketch code. It seems likely that the sketch code was written for use with a different library. There are thousands of libraries, so for any common header file name such as "BluetoothSerial.h", there might be multiple libraries. So it isn't always guaranteed that you can find the right library by just installing the one from Library Manager that has a name matching the header file.

1 Like

Thank you for the reply! I didn't find the code from anywhere specifically, but took parts of code from other codes as none of the other codes I found would work for my end goal. I looked over the libraries I included and felt that ArduinoBLE would be a better library. However, I'm not sure how changing the library would affect the code I have, would I need to change variable declarations such as:
BluetoothSerial SerialBT;
because I'm no longer using the BluetoothSerial library. Would it be beneficial for me to just keep the BluetoothSerial library as well, or would that just be a waste of space?

The "BluetoothSerial" library is for use with a Bluetooth Classic to serial adapter module like the "HC-05" or "HC-06" connected to the board.

The "ArduinoBLE" library is to use the BLE (Bluetooth Low Energy) radio that is built-in to your UNO R4 WiFi board.

So these are two very different things.

If you don't have an external Bluetooth Classic to serial module connected to the UNO R4 WiFi, then the BluetoothSerial library would never have worked for you, even if you corrected the errors in the code.

Yes. You will need to completely rewrite all the communication code in your sketch. BLE works very differently than the simple Bluetooth Classic serial interface. You should carefully study the documentation for the ArduinoBLE library here:

https://docs.arduino.cc/libraries/arduinoble/

and the relevant example sketches that are listed under the File > Examples > ArduinoBLE menu in Arduino IDE after you install the ArduinoBLE library.

The amount of hard drive space the library uses is insignificant. However, the library does take up space in the Arduino IDE menus and might cause you confusion in the future. So if you don't own a Bluetooth Classic to serial module, or otherwise don't plan to use the library, then it is a good idea to tidy up by deleting it. I'll provide instructions you can follow to do that:

  1. Select Sketch > Include Library > Manage Libraries... from the Arduino IDE menus to open the "Library Manager" view in the left side panel.
  2. Type BluetoothSerial in the "Filter your search..." field.
  3. Find the "BluetoothSerial" entry in the list of search results.
  4. Hover the mouse pointer over the "BluetoothSerial" entry.
  5. You will see a ●●● icon appear near the top right corner of the library entry. Click on that icon.
    A context menu will open.
  6. Select "Remove" from the menu.
    An "Uninstall" dialog will open.
  7. Click the "YES" button in the "Uninstall" dialog to confirm that you want to uninstall the library.
    The dialog will close.
  8. Wait for the uninstall process to finish, as indicated by a notification at the bottom right corner of the Arduino IDE window:

    ⓘ Successfully uninstalled library ...

1 Like

Thank you so much! I've made a lot of progress, but I am getting a new error. I try to create a BLEServer object called server, however that is throwing a error. I only imported ArduinoBLE and later added BLEDevice just for good measure, but from what I've read so far ArdunioBLE replaced the libraries such as BLEDevice, BLEServer, ect., would this be incorrect? I did try importing BLEServer and BLEUtils just incase I was wrong about this, but I could not find it in the library manager.

Here's my updated code:

#include<Arduino.h>
#include <ArduinoBLE.h>
#include <BLEDevice.h>

const int relayPin=3; //Digital pin 3 connected to IN3 of the relay
const int powerPin=4; //Digital pin 4 (used as 5V power pin)

BLEServer server;
BLEService service("ActuatorControl");
BLECharacteristic characteristic("ActuatorCommand", BLERead|BLEWrite);

void setup(){
  Serial.begin(9600);
  BLE.begin();
  server.setAdvertisedService(service);
  service.addCharacteristic(characteristic);
  server.begin();
  pinMode(relayPin, OUTPUT);
  pinMode(powerPin, OUTPUT);
  digitalWrite(powerPin, HIGH);
}

void loop(){
  BLEDevice central=BLE.central();
  if(central){
    if(characteristic.valueChanged()){
      String command=characteristic.readValue();
      command.trim(); //Remove any whitespace
      if (command=="U"||command=="R"){ //if app gives input "U" or "R"
            digitalWrite(relayPin, HIGH); //Make relay go up (Activate relay)
            delay(1000);//Delay (Speed control) (wait 1 sec)
            digitalWrite(relayPin, LOW); //turn off relay
          }
          else if (command=="D"||command=="L"){
            digitalWrite(relayPin, LOW); //Make relay go down (Deactivate relay)
            delay(2000);//Delay (Speed control) (wait 2 sec)
            digitalWrite(relayPin, HIGH); //Make relay go down (Deactivate relay)
          }
          else if (command=="S"){
            digitalWrite(relayPin, LOW); //Stop actuator
          }
    }
  }
  BLE.poll();
}

Here's the new error:

/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:145:1: error: 'BLEServer' does not name a type; did you mean 'BLEService'?
 BLEServer server;
 ^~~~~~~~~
 BLEService
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:147:69: error: no matching function for call to 'BLECharacteristic::BLECharacteristic(const char [16], int)'
 BLECharacteristic characteristic("ActuatorCommand", BLERead|BLEWrite);
                                                                     ^
In file included from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLEService.h:23:0,
                 from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLEDevice.h:25,
                 from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/local/BLELocalDevice.h:23,
                 from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/ArduinoBLE.h:23,
                 from /Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:139:
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:125:3: note: candidate: BLECharacteristic::BLECharacteristic(BLERemoteCharacteristic*)
   BLECharacteristic(BLERemoteCharacteristic* remote);
   ^~~~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:125:3: note:   candidate expects 1 argument, 2 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:116:3: note: candidate: BLECharacteristic::BLECharacteristic(BLELocalCharacteristic*)
   BLECharacteristic(BLELocalCharacteristic* local);
   ^~~~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:116:3: note:   candidate expects 1 argument, 2 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:50:3: note: candidate: BLECharacteristic::BLECharacteristic(const BLECharacteristic&)
   BLECharacteristic(const BLECharacteristic& other);
   ^~~~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:50:3: note:   candidate expects 1 argument, 2 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:49:3: note: candidate: BLECharacteristic::BLECharacteristic(const char*, uint16_t, const char*)
   BLECharacteristic(const char* uuid, uint16_t permissions, const char* value);
   ^~~~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:49:3: note:   candidate expects 3 arguments, 2 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:48:3: note: candidate: BLECharacteristic::BLECharacteristic(const char*, uint16_t, int, bool)
   BLECharacteristic(const char* uuid, uint16_t permissions, int valueSize, bool fixedLength = false);
   ^~~~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:48:3: note:   candidate expects 4 arguments, 2 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:47:3: note: candidate: BLECharacteristic::BLECharacteristic()
   BLECharacteristic();
   ^~~~~~~~~~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:47:3: note:   candidate expects 0 arguments, 2 provided
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino: In function 'void setup()':
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:152:3: error: 'server' was not declared in this scope
   server.setAdvertisedService(service);
   ^~~~~~
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:152:3: note: suggested alternative: 'service'
   server.setAdvertisedService(service);
   ^~~~~~
   service
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino: In function 'void loop()':
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:163:23: error: 'class BLECharacteristic' has no member named 'valueChanged'; did you mean 'valueLength'?
     if(characteristic.valueChanged()){
                       ^~~~~~~~~~~~
                       valueLength
/Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:164:47: error: no matching function for call to 'BLECharacteristic::readValue()'
       String command=characteristic.readValue();
                                               ^
In file included from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLEService.h:23:0,
                 from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLEDevice.h:25,
                 from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/local/BLELocalDevice.h:23,
                 from /Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/ArduinoBLE.h:23,
                 from /Users/aayonavikram/Documents/Arduino/Linear_Actuator_Control/Linear_Actuator_Control.ino:139:
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:62:7: note: candidate: int BLECharacteristic::readValue(uint8_t*, int)
   int readValue(uint8_t value[], int length);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:62:7: note:   candidate expects 2 arguments, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:63:7: note: candidate: int BLECharacteristic::readValue(void*, int)
   int readValue(void* value, int length);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:63:7: note:   candidate expects 2 arguments, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:64:7: note: candidate: int BLECharacteristic::readValue(uint8_t&)
   int readValue(uint8_t& value);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:64:7: note:   candidate expects 1 argument, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:65:7: note: candidate: int BLECharacteristic::readValue(int8_t&)
   int readValue(int8_t& value);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:65:7: note:   candidate expects 1 argument, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:66:7: note: candidate: int BLECharacteristic::readValue(uint16_t&)
   int readValue(uint16_t& value);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:66:7: note:   candidate expects 1 argument, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:67:7: note: candidate: int BLECharacteristic::readValue(int16_t&)
   int readValue(int16_t& value);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:67:7: note:   candidate expects 1 argument, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:68:7: note: candidate: int BLECharacteristic::readValue(uint32_t&)
   int readValue(uint32_t& value);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:68:7: note:   candidate expects 1 argument, 0 provided
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:69:7: note: candidate: int BLECharacteristic::readValue(int32_t&)
   int readValue(int32_t& value);
       ^~~~~~~~~
/Users/aayonavikram/Documents/Arduino/libraries/ArduinoBLE/src/BLECharacteristic.h:69:7: note:   candidate expects 1 argument, 0 provided

exit status 1

Compilation error: 'BLEServer' does not name a type; did you mean 'BLEService'?

You need to spend more time studying the documentation and examples for the ArduinoBLE library.

BLE is a very complex subject matter and ArduinoBLE is a very complex library, so your current approach of copy/pasting together a Frankenstein monster of random snippets you found on the Internet is not a valid approach.

I think the example sketch from File > Examples > ArduinoBLE > Peripheral > LED will be a good reference for your project (the UNO R4 WiFi board will be configured as a "peripheral device"). If you upload that sketch to the UNO R4 WiFi board, you can then use a general purpose BLE app to control the LED on the board. Even though it isn't exactly what you are trying to do in your real project, a simple working project can be a great starting point.

I have used this app:

In case that one isn't to your liking for some reason you could also look at this alternative app:

I haven't tried that one, but I think some of the Arduino community members do use it.

1 Like

Thank you so much! nRF Connect was extremely helpful!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.