Trying to run relooping switch cases via bluetooth and button

Hi,

i want to control 2 Motors with a button and via bluetooth. They should trigger a loop within a switch statement. I want to switch between 3 sequences(loops).
Switching between the loops via button is working well and every sequence(loop1,loop2,loop3) is looping fine via button.
But when i try to do the same with a serial input, it reads and triggers the sequence for a second and then gets back to the button loop. But what i want is, that the sequence I trigger via serial is doing the same like the button function. It should loop the selected sequence till i send another serial or push the button to change it. I hope you can understand what I want, because of my english knowledge it is not so easy for me to describe it.

I know i have to put something within the bluetooth void that tells the arduino to reloop the sequence till there is another input like I did it with the button. And I tried to figure it out since days without making any progress. So i would really appreciate your help :slight_smile:

This is my code so far.

#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
  #include <SoftwareSerial.h>
#endif

#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "BluefruitConfig.h"

int nextTime1 = 1000;  // Do this every second or 1000 milliseconds
int nextTime2 = 600;  // Do this every 600 milliseconds
long int goTime1, goTime2;

//Motor Pin Variables
int motorPin1 = 5;//Big Motor
int motorPin2 = 6;//Small Motor
                                                                      
int buttonPin = 11;  // Button pin variable

int val = 0; // variable to read button pin value

int sequence = 1; // variable to hold current sequence


/*=========================================================================
    APPLICATION SETTINGS

    FACTORYRESET_ENABLE     Perform a factory reset when running this sketch
   
                            Enabling this will put your Bluefruit LE module
                            in a 'known good' state and clear any config
                            data set in previous sketches or projects, so
                            running this at least once is a good idea.
   
                            When deploying your project, however, you will
                            want to disable factory reset by setting this
                            value to 0.  If you are making changes to your
                            Bluefruit LE device via AT commands, and those
                            changes aren't persisting across resets, this
                            is the reason why.  Factory reset will erase
                            the non-volatile memory where config data is
                            stored, setting it back to factory default
                            values.
       
                            Some sketches that require you to bond to a
                            central device (HID mouse, keyboard, etc.)
                            won't work at all with this feature enabled
                            since the factory reset will clear all of the
                            bonding data stored on the chip, meaning the
                            central device won't be able to reconnect.
    -----------------------------------------------------------------------*/
    #define FACTORYRESET_ENABLE      1
/*=========================================================================*/


// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);

Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
                      BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/

/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
// Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);

/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
//                             BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
//                             BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);


// A small helper
void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}

/**************************************************************************/
/*!
    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)
*/
/**************************************************************************/


void setup()
{

  //Set each pin connected to an LED to output mode (pulling high (on) or low (off)
  
      pinMode(motorPin1,OUTPUT);//Set Motor as Output
      pinMode(motorPin2,OUTPUT);//Set Motor as Output
      pinMode(buttonPin, INPUT); // Set button pin to be an input

     goTime1 = millis();
     goTime2 = millis();
}                                 
  
 

 

void loop()                     // run over and over again
{                           
if(millis() >= goTime1) button();
if(millis() >= goTime2) bluetooth();
}

//Configuration of gotime

void button(){
                             
       // check if button pressed
        val = digitalRead(buttonPin);
        if(val == LOW) 
        {
            if(sequence == 3) // if sequence is at 3 already, make it 1 again
            {
               sequence = 1;
            } else
                {
                  sequence++; // otherwise go to the next sequence
                }
        }
                   
         switch(sequence)
                 {
                     case 1:
                     Loop1();
                     Serial.println("Loop1");
                     break;
                     case 2:
                     Loop2();
                     Serial.println("Loop2"); 
                     break;
                     case 3:
                     Loop3();
                     Serial.println("Loop3");
                     break;
                 }
                      
}  

void bluetooth(){
if (Serial.available() > 0) {
    int inByte = Serial.read();

    switch (inByte) {
      case '1':
        Loop1();
        Serial.println("Modus 1"); 
        break;
      case '2':
        Loop2();
        Serial.println("Modus 2");
        break;
      case '3':
        Loop3();
        Serial.println("Modus 3");
        break;
    }
  }
  }

  
  
//Configuration of the different Motor Loops

void Loop2(){
  int delayTime = 100; 
  digitalWrite(motorPin1, HIGH);  
  delay(delayTime);              
  digitalWrite(motorPin2, HIGH);  
  delay(delayTime);              
  digitalWrite(motorPin2, LOW);  
  delay(delayTime);               
  digitalWrite(motorPin2, LOW);  
  delay(delayTime);                
  digitalWrite(motorPin1, LOW);  
  delay(delayTime);            
  digitalWrite(motorPin2, HIGH);  
  delay(delayTime);               
  digitalWrite(motorPin2, HIGH);  
  delay(delayTime);               
  digitalWrite(motorPin2, LOW);  
  delay(delayTime);               
 }

void Loop1(){
 int delayTime = 1000;
  digitalWrite(motorPin1, LOW);  
  digitalWrite(motorPin2, LOW);
  delay(delayTime);  

}
 

void Loop3(){
   int delayTime = 100; 
  digitalWrite(motorPin1, HIGH);  
  digitalWrite(motorPin2, HIGH); 
  delay(delayTime);               
   digitalWrite(motorPin1, LOW);  
  digitalWrite(motorPin2, LOW);  
  delay(delayTime);              
}

I think you need to run the code more like this:

  1. Check Serial.available();
  2. If there is something available, set a variable “byte state” to “serial.read()”; else{ continue main loop}
  3. Run the function(state) in main loop.
  4. Recheck Serial.available()

In the button() function (dumb name), you reset sequence only when the switch is pressed and then call the various functions regardless of whether the switch is pressed, or not.

In the bluetooth() function (another dumb name), you call the various functions only when there is new serial data.

You need to be consistent.