What's wrong with my simple program?

I tried the 60 seconds one just now. it still shows that error after I press watering for several times.

I think if I control it ("watering" and "off")totally by myself will be easier(not the semi-auto style)?

Hi Kitty

Yes, Arduino Code Scripts are called Sketches. No idea why :upside_down_face:

It's useful to have that error Block in the App so we can see what happens. Are you sure the error message is identical to the screenshot you posted? It does suggest that something is very wrong but still I'm not sure that is specifically Bluetooth.

I think you need to avoid the off button as you have it. It does not 'switch off' anything, it sends the servo to it's home position. If the servo is "sent home" too often when it already is home, that could damage it over a period of time (unless you have a servo that's designed to avoid such issues).

I did add a button, Button_Exit, which disconnected Bluetooth.

That itself might be the issue, re ABG's note about flooding the AI2 side with messages. If you press watering more often than the Sketch Loop time interval, that could be the issue.

So I'm going to modify your App code to see if we can make the process more robust.

Thanks a lot, ChrisWard, I'm trying to understand your code.

And this is the new error it shows.

Hi Kitty.

OK, I have changed the App to try to ensure there is no conflict with the Sketch. I have done a bit of component renaming so it's easier to understand each function:

  1. Prevent the User sending a command when Arduino is busy by disabling buttons.
  2. More information of the process status.
  3. Change colour of Buttons to show availability.

I'm unable to test here, no space available for my Ardunio lab at the moment.

waterplant_ed2.aia (7.7 KB)

WaterPlant_Ed2.txt (1.9 KB)

//WaterPlant_Ed2.ino 01/11/2020 12:26:13

#include<Servo.h>
Servo servomotor1;
//833 air
//632 water

//vars
char    IncomingValue = 0;
const int    AirValue = 833; //you need to change this value that you had recorded in the air
const int  WaterValue = 632; //you need to change this value that you had recorded in the water
int         intervals = (AirValue - WaterValue) / 3;
int soilMoistureValue = 0;
int     ServoPosition = 1; //1 = original home position, 0 = watering

unsigned int igUpdateTime;

void setup()
{
         servomotor1.attach(12);
         servomotor1.write(120);//set @ home position
              Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
         igUpdateTime = millis();
}

void loop()
{
         //Excute loop every 0.5 minute - if this time interval is changed, the App Timers must be changed too
	     if((millis() - igUpdateTime) > 30000)
         {
               igUpdateTime = millis();

               if(Serial.available() > 0)
               {
                     IncomingValue = Serial.read();

                     if(ServoPosition == 0)
                     {
                         servomotor1.write(120);//original position
                         ServoPosition = 1;
                     }

                     switch (IncomingValue)
                     {
                         case '1':
                                  servomotor1.write(0);//watering
                                  ServoPosition = 0;
                                  break;
                         case 'M':
                                  soilMoistureValue = analogRead(A0);
                                  Serial.print(soilMoistureValue);
                                  Serial.println();
                                  break;
                     }
               }
         }
}

Hi ChrisWard,
The programs seem complicated...
I tested the programs, but when I pressed the Get moisture button, it shows an error.
And how can I set the servo back to the original position? The good news is at the very beginning, it always backs to the original position. And sometimes, when the status shows it is watering and the watering is done,actually there is no response for the servo.

Hi Kitty

What is the model name of your Arduino? Currently, we are trying to send and receive via the same channel, but it would be interesting to have separate send and receive channels.

The code is not really complicated, if you study it carefully from button click to action, you should be able to understand it. :grin:

Answered your own question there.....the Sketch code does it for you.

Do you mean you press Button_Water, the "WATERING DONE" message is shown, but the servo didn't actually perform the task? When exactly, does that happen - is it OK before Button_GetMoisture was tapped? (i.e. before there was an error?).

Hi Kitty

Well, Error 516 is a temporary disconnect. It could be caused by not having sufficient power. That might explain the servo issues. Questions:

  1. Ardunio model name?
  2. Specification Sheet for the Servo?
  3. How is your Arduino being powered?
  4. How is the servo being powered?

It could also be caused by flooding the Arduino with messages, but I think the new App code should avoid this.

  1. I use Arduino Uno. I don't know what is Arduino model name?
  2. I use SG 90 servo motordatasheet
  3. I use USB
    4 .The servo is powered by Arduino.

Yes, it didn't actually perform the task. it happened before there was an error.

But actually, it didn't get back to the original position.

Possibly because it does not have enough power.

OK - the model name is "UNO".

About 3 - "I use USB". What is that USB connected to?
a) A mains electricity power supply
b) A battery pack
c) A computer

The USB is connected to a computer.

An interesting thing - your Sketch says the Servo Signal wire (yellow) is attached to pin 12, but the spec says it is a PWM (Pulse Width Modulated) signal, so it should be connected to pin ~3.

Another interesting thing is the spec does not tell us the milliamps required, only the voltage (circa 5V which you can take directly from the Arduino).

OK - not enough power!
From my website (ProfessorCad: Tips & Tricks)

Specification of an AC To DC Power Adaptor For Arduino: 9v to 12V max DC output, 250mA to 1A max current output, 2.1mm diameter plug which is centre pin positive.
If you are connecting kit like lots of LEDs, LCDs, servo motors etc, the Adapter should supply at least 500mA, upto 1A max.

So there we have it, this is the backbone of the issues you are seeing.

Do you mean that the Arduino UNO didn't get enough power? If this is the problem, why I can control the servo for several times.

That's what I think, yes - it does not have enough power to drive the servo properly, for a reasonable length of time. By default, USB power output is 5V, 100 milliamps. I understand the milliamps can be increased to 500, do not know how to measure that is happening though as the device plugged in should be demanding the power required.

...ah, if you have a fairly modern PC, at least one of your USB ports should be designated as a 'charging port' (for charging your phone or powering an external device). So that's the port to connect your USB cable to - though if you already have a suitable power adapter, I would use that.

This utility program (free) from NirSoft can be used to see everything about your USB ports:

https://www.nirsoft.net/utils/usb_devices_view.html

If your device pulls too much power from the PC USB Port, the PC will interrupt the connection, so that would fit with the servo working at first but quickly failing.

Note also that it's very import to attach the servo signal cable to the correct Arduino pin - that alone fixes problems with controlling the servo.