Bluetooth servo control

Hi All …
Ok,
I binned the RTC idea for the moment until I understand the Bluetooth command instruction from my phone and get that running properly.

Now I Arduino is listening for the Character ‘y’ from the serial monitor ad this should cycle the servo once only. This is being received as my servo is moving after it has new code uploaded or is reset, but it returns to the park position it loops again. what am I doing wrong ???

now I have a program loop again and I think this is a bit easier to solve than my last mess of code.
the pos =265 anb pos =56 are the stop start positions for the servo to make the feeder dispense food properly to answer that question :slight_smile:

#include <Servo.h>
const int kservosignalpin = 8;
Servo myservo;
int pos = 0;
char Feedornot;
 void setup()
{Serial.begin(9600);}
 
void OpenFeeder(){
    myservo.attach(kservosignalpin);// attaches the servo on pin 9 to the servo object
    myservo.write(26); 
    delay (5000);
    myservo.write(56);
    delay (5000);}
void Feederclosed(){
  myservo.attach(kservosignalpin); 
  myservo.write(26); 
  myservo.detach();}
  
void loop() 
      
{if(Serial.available())
 Feedornot = Serial.read();
       if (Feedornot =='y')
       OpenFeeder();
       else
      Feederclosed();
         }

What will the value of Feedornot be the next time through loop() after you have received 'y' for the first time ?

Once you go in to OpenFeeder( ) you need to provide a way to get out. [ State machine ]

Hi Runaway Pancake How do I go about this ??? I though the myservo.detach would resolve this ???

http://hacking.majenko.co.uk/finite-state-machine

{if(Serial.available())  //if there is input on serial
 Feedornot = Serial.read();   //read the available byte
       if (Feedornot =='y')      //if the Feeedornot variable is 'y' even if no serial data was available 
                                 //and it is still 'y' from the last time through loop()
       OpenFeeder();            //open the feeder
       else
      Feederclosed();
         }

After reading the serial data, and deciding what to do, you could set Feedornot to NULL so that next time through loop(), Feedornot is not what you read from the serial port the last time there was something to read.

Or, as UKHeliBob shows, only act on the data when there is data to act on.

Im confused,

What UKHeliBob posted is exactly what SpookyFlash has in his original post, isnt it?

Marciokoko: Im confused,

What UKHeliBob posted is exactly what SpookyFlash has in his original post, isnt it?

The code is as was posted, but read the comments that I added to it. They explain what is causing the problem, namely that Feedornot may still equal 'y' even if no serial data was available to be read as it still retains it's value from the last time that it was read.

Oh, I come from ObjC and only recently swift. So those 2 it's are not nested? Because if they are, the first if would force the program to only run the second if when a data is input.

The only command executed if the test for serial data returns true is Feedornot = Serial.read();hence if no serial data is available Feedornot retains its previous value.

In order to nest the commands the code to be executed when the first test returns true must be put in braces. If that were not the case then how would the compiler know which commands should be executed ?

Simple serial servo test code that might be close to what you want.

//zoomkat serial toggle test 6-20-2014
#include <Servo.h>
int ledPin = 13;
char c; // = 0;
Servo servo1;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  servo1.attach(7); //pin for servo control signal
  servo1.write(20); //initial position
  Serial.println("serial on/off test 6-20-2014"); // so I can keep track
}
   
void loop()
{
  if (Serial.available() > 0) 
  {
    c = Serial.read();
    if (c == 'y')     // assuming you send character '1', not a byte with value 1 
    {
       digitalWrite(ledPin, HIGH);
       Serial.println(c);
       servo1.write(160);
    }
    if (c == 'z') //send z to reset position
    {
       digitalWrite(ledPin, LOW);
       Serial.println(c);
       servo1.write(20);
    }
  }
}