Pistola de burbujas de jabón

Holaa todos!
Encontré un mini programa para hacer medio "autónoma" una pistola de hacer burbujas de jabón....

Aquí el enlace donde lo encontré:

La intención es que funcione mediante un servo y arduino UNO o Nano, para no tener que estar accionando el gatillo, a mi sobrina....le encanta la idea! solo que.....el mini programa, no funciona!!
Aquí el código:

#include <Servo.h>

#define SERVO_PIN 9
#define SERVO_POS_OFF 40
#define SERVO_POS_ON  100

Servo servo;
String buffer = "";

boolean bubbling = false;
unsigned long bubbleLoopCount = 0;

void setup(){
    servo.attach(SERVO_PIN);
    servo.write(SERVO_POS_OFF);

    Serial.begin(9600);
    buffer.reserve(64);
    Serial.println("Ready!");
}

void loop(){
    readSerial();

    if(bubbling){
        bubbleUpdate(); 
    }
}

void readSerial(){
    while(Serial.available()){
        char inChar = (char)Serial.read();
        if(inChar == '\n') {
            processCommand(buffer);
            buffer = "";
        } else {
            buffer += inChar;
        }
    }
}

void processCommand(String command){
    if(command == "bubbles"){
        bubbling = true;
    }
}

void bubbleUpdate(){
    //Naive state changes, modify values to change timing
    //Detach servo at cycle end to save power.
    if(bubbleLoopCount == 0){
        servo.attach(SERVO_PIN);
        servo.write(SERVO_POS_ON); 
    }
    if(bubbleLoopCount == 80000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 110000){
        servo.write(SERVO_POS_ON);
    }
    if(bubbleLoopCount == 200000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 250000){ 
        bubbleLoopCount = 0;
        bubbling = false;
        servo.detach();
        return; 
    }

  bubbleLoopCount++;
}

Quizás sea por algún error aunque......a la hora de compilar, lo hace sin ningún error.....
Alguien sabría decirme cómo arreglarlo para que funcione??.....mi sobrina y yo os lo agradecemos!!
Saludos!

Digo yo, probaste enviarle bubbles como dice el código por el puerto serie?

Justamente esta esperando eso.

De este modo ya no esperará comando por puerto serie y funcionará desde el arranqué.
Podrias poner un pulsador o un switch tmb que lo determine.

#include <Servo.h>

#define SERVO_PIN 9
#define SERVO_POS_OFF 40
#define SERVO_POS_ON  100

Servo servo;
String buffer = "";

boolean bubbling = false;
unsigned long bubbleLoopCount = 0;

void setup(){
    servo.attach(SERVO_PIN);
    servo.write(SERVO_POS_OFF);

    Serial.begin(9600);
    buffer.reserve(64);
    Serial.println("Ready!");
    bubbling = true;
}

void loop(){
    //readSerial();

    if (bubbling){
        bubbleUpdate(); 
    }
}

void readSerial(){
    while(Serial.available()){
        char inChar = (char)Serial.read();
        if(inChar == '\n') {
            processCommand(buffer);
            buffer = "";
        } else {
            buffer += inChar;
        }
    }
}

void processCommand(String command){
    if(command == "bubbles"){
        
    }
}

void bubbleUpdate(){
    //Naive state changes, modify values to change timing
    //Detach servo at cycle end to save power.
    if(bubbleLoopCount == 0){
        servo.attach(SERVO_PIN);
        servo.write(SERVO_POS_ON); 
    }
    if(bubbleLoopCount == 80000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 110000){
        servo.write(SERVO_POS_ON);
    }
    if(bubbleLoopCount == 200000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 250000){ 
        bubbleLoopCount = 0;
        bubbling = false;
        servo.detach();
        return; 
    }

  bubbleLoopCount++;
}

Eso está genial!!!
Sí, casi mejor con un pulsador para encender y apagar.....

Mira como agregarle un switch a tu proyecto para que controles cuando funciona y cuando no.

Luis llamas LEER UN PULSADOR CON ARDUINO

Modificación con un switch. (no con pulsador!!)

Conectado a pin 2 entre GND y el PIN.

#include <Servo.h>

#define SERVO_PIN 9
#define SERVO_POS_OFF 40
#define SERVO_POS_ON  100

Servo servo;
String buffer = "";

boolean bubbling = false;
unsigned long bubbleLoopCount = 0;
const byte botonPin = 2;

void setup(){
    servo.attach(SERVO_PIN);
    servo.write(SERVO_POS_OFF);
    pinMode(boton, INPUT_PULLUP);
    Serial.begin(9600);
    buffer.reserve(64);
    Serial.println("Ready!");
}

void loop(){
    
    if (digitalRead(botonPin)) 
        bubbling = true;
    else 
        bubbling = false;

    if (bubbling){
        bubbleUpdate(); 
    }
}

void bubbleUpdate(){
    //Naive state changes, modify values to change timing
    //Detach servo at cycle end to save power.
    if(bubbleLoopCount == 0){
        servo.attach(SERVO_PIN);
        servo.write(SERVO_POS_ON); 
    }
    if(bubbleLoopCount == 80000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 110000){
        servo.write(SERVO_POS_ON);
    }
    if(bubbleLoopCount == 200000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 250000){ 
        bubbleLoopCount = 0;
        bubbling = false;
        servo.detach();
        return; 
    }

  bubbleLoopCount++;
}

Versión con pulsador

#include <Servo.h>

#define SERVO_PIN     9
#define SERVO_POS_OFF 40
#define SERVO_POS_ON  100

Servo servo;

const byte botonPin = 2;        // Elige el pin que sea de tu comodidad. Pulsador entre GND y Pin
boolean bubbling = false;
boolean estado, estadoAnt = false;
unsigned long bubbleLoopCount = 0;

void setup(){
    servo.attach(SERVO_PIN);
    servo.write(SERVO_POS_OFF);
    pinMode(boton, INPUT_PULLUP);
    Serial.begin(9600);
    Serial.println("Ready!");
}

void loop(){
    
    estado = digitalRead(botonPin);
    if (estado && !estadoAnt) 
        bubbling != bubbling;
    estadoAnt = estado;

    if (bubbling){
        bubbleUpdate(); 
    }
}

void bubbleUpdate(){
    //Naive state changes, modify values to change timing
    //Detach servo at cycle end to save power.
    if(bubbleLoopCount == 0){
        servo.attach(SERVO_PIN);
        servo.write(SERVO_POS_ON); 
    }
    if(bubbleLoopCount == 80000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 110000){
        servo.write(SERVO_POS_ON);
    }
    if(bubbleLoopCount == 200000){
        servo.write(SERVO_POS_OFF);
    }
    if(bubbleLoopCount == 250000){ 
        bubbleLoopCount = 0;
        bubbling = false;
        servo.detach();
        return; 
    }

    bubbleLoopCount++;
}

Muchísimas gracias por la ayuda!!
Ya funciona....mi sobrina se pondrá muy contenta!!
Saludos!!