Motor position control with Ultrasonic Sensor

Hey Guys

Very new to this and have taken on a rather ambitious project of creating a AC single phase motor control ciruit to raise a chair to preset levels.

Using a URM37 ultrasonic in PWM mode ( am open to the idea of using TTL)

The Idea works perfectly with a variable resistor as the position sensor however due to budget constraints a cannot purchase a linear potentiometer big enough to do the job. So I am using an ultrasonic sensor. Trying to integrate the code into the motor control is casing an issue, I get error: expected unqualified-id before ‘{’ token after initiating the sensor in the void loop.

Please help sketch is attached

Motor_Ultrasonic.ino (6.31 KB)

You will have to tell us the line number that has the problem. That problem usually means you forgot a ; or something else earlier in the code.

...R

Hey

Ok so I managed to get everything working in the orginal code with buttons.

Now the next challenge! I am needing to trigger the various states via UDP strings. I am receiving them but they are not triggering the appropriate function?

Been staring at this for hours, any help would be really appreciated!

Motor_Control_UDP_So_Close.txt (9.7 KB)

Can you show a few of the strings as they are received and the code you are using to test them.

Because you have all your code in loop() (or most of it) I find it hard to see exactly what piece is causing trouble.

...R

This is what it is giving out on the serial monitor, if you look further down i have included what it gives with the sketch which uses buttons instead of udp:

Ping: 7cm
Ping: 7cm
Received packet of size 9
From 192.168.0.100, port 34226
Contents:
button2&&
Current = 407 Goal = 45
IDLE
Ping: 7cm
Ping: 7cm
Ping: 6cm
Ping: 7cm
Received packet of size 9
From 192.168.0.100, port 42094
Contents:
button2&&
Current = 407 Goal = 45
IDLE
Ping: 7cm
Ping: 7cm
Ping: 7cm
Ping: 7cm
Received packet of size 9
From 192.168.0.100, port 43135
Contents:
button2&&
Current = 407 Goal = 45
IDLE
Ping: 7cm
Ping: 7cm
Ping: 7cm
Ping: 7cm
Received packet of size 9
From 192.168.0.100, port 40356
Contents:
button2&&
Current = 407 Goal = 45
IDLE
Ping: 7cm
Ping: 7cm
Ping: 7cm
Ping: 6cm
Received packet of size 9
From 192.168.0.100, port 38360
Contents:
button2&&
Current = 387 Goal = 45
IDLE
Ping: 7cm
Ping: 7cm
Ping: 7cm
Ping: 7cm
Ping: 7cm
Received packet of size 9
From 192.168.0.100, port 44058
Contents:
button2&&
Current = 407 Goal = 45
IDLE
Ping: 7cm
Ping: 7cm
Ping: 7cm
Ping: 7cm

Button Sketch:

IDLE
IDLE
Current = 411 Goal = 800
IDLE
IDLE
Current = Ping: 7cm
411 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = Ping: 7cm
407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE
IDLE
Current = 407 Goal = 800
IDLE

You have your UDP buffer defined like this:

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

Then you are comparing like this:

if (packetBuffer == ("button1&&"))

That's not going to work. Comparing a char array with a string literal doesn't do what you'd expect. It would be best to convert your char array to a string object first, which does support the comparison operator.

In fact, looking at your code, you already do this:

String results = (packetBuffer);

So try changing your comparison as follows:

if (results == "button1&&")

Depending on the real content of your UDP packets, you may need to include a terminating newline or carriage return in the comparison, something like:

if (results == "button1&&\r")

// or ...

if (results == "button1&&\n")

// or even ...

if (results == "button1&&\r\n")

Another alternative is to use the string comparison function "strcmp", which avoids creating a string object. Do it like this:

if (strcmp(packetBuffer, "button1&&") == 0)

Let us know what happens?

Thanks for the Help, I got rid of the ping print line which makes serial monitor a bit more friendly.

I have thanged the code to look like this:

if (results == “button1&&\r\n”) {
// set new goal position
goalPosition = 600;

if (goalPosition > CurrentPosition) {
Retracting = false;
Extending = true;
digitalWrite(relay1Pin, HIGH); //Run Coil
digitalWrite(relay2Pin, LOW); //Forward Start Coil
digitalWrite(relay3Pin, LOW); //Reverse Start Coil
Serial.println(“Extending”);
}
else if (goalPosition < CurrentPosition) {
Retracting = true;
Extending = false;
digitalWrite(relay1Pin, LOW); //Forward Run Coil
digitalWrite(relay2Pin, HIGH); //Reverse Run Coil
digitalWrite(relay3Pin, HIGH); //Reverse Start Coil
Serial.println(“Retracting”);

and am getting this on the monitor now:
Received packet of size 14
From 192.168.0.100, port 36229
Contents:
button1 &&\r\n
Current = 315 Goal = 0
IDLE
Received packet of size 14
From 192.168.0.100, port 49053
Contents:
button1 &&\r\n
Current = 311 Goal = 0
IDLE
Received packet of size 14
From 192.168.0.100, port 38557
Contents:
button2 &&\r\n
Current = 315 Goal = 0
IDLE

It seems that it is not changing the goal int:

// variables will change:
int sensorValue = 0; // variable to store the value coming from the sensor

int goalPosition = 0;
int CurrentPosition = 0;
boolean Extending = false;
boolean Retracting = false;

any suggestions?

At that IP crap seems to have nothing to do with your ultrasound or your motor.

The question I asked earlier was looking for a 6 or 8 line answer not a whole screen full.

Based on what @RogerRowland said, it seems like you are unsure of where your data is stored.

You need to figure out exactly where the data is stored and how that variable is defined.
You need to give us an exanple of exactly what data is received.
Then you need to show us a 1-line example of the code you are using to test what the variable contains.

At this stage, don't bother with anything else.

It is not a good idea to use Strings (capital S) on an Arduino. Because of its limited SRAM they can cause all sorts of memory corruption. You should use strings (small s) instead. strings are arrays of char terminated with a byte value 0

...R

@Robin2
Sorry for the long reply, kinda new to this whole thing and stuggling with this.
@RogerRowland
it seems that (strcmp(packetBuffer, “chairbutton1&&”) == 0) has done the job! it now reads the instruction and sets the goalPosition to what it needs to be, only problem that I still have now is illustrated below, it seems to hold on to the previous value until you send it again?:

Received packet of size 14
From 192.168.0.3, port 45141
Contents:
chairbutton2&&
Current = 339 Goal = 0
Extending
IDLE
IDLE
Received packet of size 14
From 192.168.0.3, port 50780
Contents:
chairbutton2&&
Current = 335 Goal = 800
Extending
IDLE
IDLE
Received packet of size 14
From 192.168.0.3, port 36361
Contents:
chairbutton1&&
Current = 339 Goal = 800
Extending
IDLE
IDLE
Received packet of size 14
From 192.168.0.3, port 54920
Contents:
chairbutton1&&
Current = 339 Goal = 600
Extending
IDLE
IDLE

Any thoughts? I can live with it, can write a double instruction into the control software (could be seen as a safety factor :p)
Otherwise thank you so much for your thoughts and time!

Motor_Contol_UDP_Working.ino (9.75 KB)