Hi, I am new to arduino and experiencing an issue with my code I could use some help with.
I am trying to create a ble system using my arduino nano 33 IOT and light blue that allows two servos to rotate to a pre determined position at the press of a button. I want the servo position to be sent via light blue but the servos to only move to those positions once a physical button is pressed.
The code is working fine without the button but as soon as I try and code the button in the servos no longer move and the monitor does not print confirmation of the imput.
Any help to get this working would be greatly appreciated.
#include<Servo.h> //include the servo library
#include <ArduinoBLE.h> //bluetooth libray
Servo servo;//create a servo object
Servo servo1;
int button = 4; //pin of the first button
int pos = 0;
int pos1 = 0; //initial position of the servo
BLEService servoService("180A"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
servo.attach(3); //pin used by the servo
servo1.attach(2);
pinMode(button, INPUT_PULLUP); //define first button as input pullup
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 IoT");
BLE.setAdvertisedService(servoService);
// add the characteristic to the service
servoService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(servoService);
// set the initial value for the characteristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE servo Peripheral");
}
/*
INPUT_PULLUP send to arduino LOW signal, so, when you press the button, you send a LOW signal to arduino
*/
void loop()
{
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
switch (switchCharacteristic.value()) { // any value other than 0
// put your main code here, to run repeatedly:
case 01:
if (digitalRead(button) == LOW) { //if Value read of the button ==LOW:
Serial.print("case 1");
pos = 60;
pos1 = 110; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
break;
case 02:
if (digitalRead(button) == LOW) { //if Value read of the button ==LOW:
Serial.print("case 2");
pos = 180;
pos1 = 69; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
break;
case 03:
if (digitalRead(button) == LOW) { //if Value read of the button ==LOW:
Serial.print("case 3");
pos = 0;
pos1 = 0; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
break;
}
}
}
}
}
}
}
}
As I said I'm new to arduino and the forums, I was unsure what catagory to place this in but if it is not correct could you guide me to which one I should be using. I will try and add the code.
@bazzers118, your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions; see About the Introductory Tutorials category. Feel free to write a tutorial once you have solved your problem
I don't fully understand the details of the issue with switchCharacteristic.written() and the button not triggering the switch case in your code, but if you are trying to delay the implementation of the last sent value from the phone until a button is pressed, here is a way to do it
#include<Servo.h> //include the servo library
#include <ArduinoBLE.h> //bluetooth libray
Servo servo;//create a servo object
Servo servo1;
int button = 4; //pin of the first button
boolean enableButton = false;
int pos = 0;
int pos1 = 0; //initial position of the servo
BLEService servoService("180A"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
servo.attach(3); //pin used by the servo
servo1.attach(2);
pinMode(button, INPUT_PULLUP); //define first button as input pullup
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 IoT");
BLE.setAdvertisedService(servoService);
// add the characteristic to the service
servoService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(servoService);
// set the initial value for the characteristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE servo Peripheral");
}
void loop()
{
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
enableButton = true;
Serial.print("Value to be implemented on button press = ");
Serial.println(switchCharacteristic.value());
}
if (enableButton == true)
{
if (digitalRead(button) == LOW) {
Serial.print("Switch case value triggered = ");
Serial.println(switchCharacteristic.value());
switch (switchCharacteristic.value()) { // any value other than 0
case 01:
Serial.println("case 1");
pos = 60;
pos1 = 110; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
case 02:
Serial.println("case 2");
pos = 180;
pos1 = 69; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
case 03:
Serial.println("case 3");
pos = 0;
pos1 = 0; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
}
}
}
}
}
}
The idea is that there will be three buttons that can be pre loaded with different servo positions from the phone and then the servos can be moved between the three positions on the fly using the buttons. The positions that the servos move to will need to change as they will be adjusting suspension settings on a bike so you can pre load three lots of settings for each ride/track.
That solved my initial problem but I have now come across another issue when trying to add multiple buttons each with their own set of values that they control. I would like each button to only opperate the two cases below it and nothing else so that once one case is loaded onto each button the three can be switched between on the fly. At the minute the first button works fine but the subsequent buttons and cases (3 onwards) do not work. I'm not sure if this makes sense but any help would be appreciated.
#include<Servo.h> //include the servo library
#include <ArduinoBLE.h> //bluetooth libray
Servo servo;//create a servo object
Servo servo1;
int button = 4;
int button1 = 5;
int button2 = 6;//pin of the first button
boolean enableButton = false;
boolean enableButton1 = false;
boolean enableButton2 = false;
int pos = 0;
int pos1 = 0; //initial position of the servo
BLEService servoService("180A"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
servo.attach(3); //pin used by the servo
servo1.attach(2);
pinMode(button, INPUT_PULLUP);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP); //define first button as input pullup
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 IoT");
BLE.setAdvertisedService(servoService);
// add the characteristic to the service
servoService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(servoService);
// set the initial value for the characteristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE servo Peripheral");
}
void loop()
{
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
enableButton = true;
enableButton1 = true;
enableButton2 = true;
Serial.print("Value to be implemented on button press = ");
Serial.println(switchCharacteristic.value());
}
if (enableButton == true)
{
if (digitalRead(button) == LOW) {
Serial.print("Switch case value triggered = ");
Serial.println(switchCharacteristic.value());
switch (switchCharacteristic.value()) { // any value other than 0
case 01:
Serial.println("case 1");
pos = 60;
pos1 = 110; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
case 02:
Serial.println("case 3");
pos = 0;
pos1 = 0; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
if (enableButton1 == true)
{
if (digitalRead(button1) == LOW) {
Serial.print("Switch case value triggered = ");
Serial.println(switchCharacteristic.value());
switch (switchCharacteristic.value()) { // any value other than 0
case 03:
Serial.println("case 3");
pos = 1;
pos1 = 1; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
case 04:
Serial.println("case 4");
pos = 18;
pos1 = 9; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
if (enableButton2 == true)
{
if (digitalRead(button2) == LOW) {
Serial.print("Switch case value triggered = ");
Serial.println(switchCharacteristic.value());
switch (switchCharacteristic.value()) { // any value other than 0
case 05:
Serial.println("case 5");
pos = 10;
pos1 = 120; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
case 06:
Serial.println("case 6");
pos = 180;
pos1 = 180; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos);
servo1.write(pos1);
enableButton = false;
break;
}
}
}
}
}
}
}
}
}
}
}
}
Your style of dealing with brackets makes it difficult to see what is going on, but your button1 and button 2 readings are nested within a conditional reading of button 1 which terminates with the enableButton = false.
Work through the logic and bracketing carefully, and make all the button press reading conditionals independent. I think you only need one enableButton boolean to make all three buttons responsive after the ble value written.
Example of nested conditionals
if (digitalRead(button) == LOW) {
if (digitalRead(button1) == LOW) {
if (digitalRead(button2) == LOW) {
}
}
}
I think that when every bracket is on its own line, and you use auto format you can better see what is independent and what is nested.