Need help making this bluetooth ball gun

Hello everyone,
I am a student doing a projet, making a bluetooth ball gun, with MIT app inventor.
It works by firing the ball into two motors, and changing the angle with a servo.
I have found a couple tutorials on how to make this work, but every tutorial is : or motor or the servo, nothing combined.

They all make use of the same function that sends a 1-byte number, but, every coding reads on the SAME value, so if I upload one value, every single thing, the servo and the motors work with the same value. I want it to send seperate values, so I can control the two motors and the one servo seperately. I also need something to replace the button I implemented for a seperate small servo, that reloads the balls, so I also need something replacing a button.

The electronical components I can use are the HC-05, a transistor or a MOSFET, capacitors, etc. But I cannot use h-bidges or those motor controller boards. The two motors work on 12V, the big servo works on 6V and the small one works on the arduino 5V, if someone is interested in that.

So summing things up, I need arduino code that combines three things I need to control with my bluetooth. These are the tutorials I followed : motor tutorial by SaMvidita Servo tutorial by ElectronicsHub

Sincerely,
Luca

what's your best attempt?

I haven't attepted to actually "combine" the features, but they work seperately. It won't work combined, that's my question, because it's reading from the same and inputting data in the same way. I think I know what I need to do, I need to go to the app and let the app constantly send three values, the speed of the motors, the angle of the servo, and the value of the "button" so like this:

speed of motor
angle of motor
on/off

and I need to actually read these values seperateley, so it sets the first line as the speed variable, the second as the angle for the servo, and I need the third to be the replacer of this: if (digitalRead(13) == HIGH) { servo_12.write(50); delay(1000); //1000 milliseconden wachten servo_12.write(0); delay(500); //500 milliseconden wachten } else { servo_12.write(0); }
I have the tinkercad link to the non- bluetooth circuit I created here please note that I am not varying the speed of the motors here, and that the comments of the code are in Dutch.

how do you know that if


is it a school project then?

Yeah it's a school project, and I know it won't work because both app codes work with this feature:


and both arduino codes read the bluetooth using Serial.read, so if it sends 180 for the degrees, it will also use that as speed variable.

you need to merge the code so that you enable the BT reception only once

the loop will then just deal with the commands, make sure you send different types of commands for different usages

instead of just sending the value as 1 byte, send a special code, like 's' for the servo and 'l' for the left motor and 'r' for the right motor and then send the byte value for either the angle or the speed

reading code will wait for a command specifier (s, l or r) and then for a byte.

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

if you want to make more complex commands, you'll need to learn how to handle the serial line, I would suggest to study Serial Input Basics to handle this. Sending the command in ASCII would also make it easier than in binary.

so as I get it, I need it to send s and then the value, so : s180 would be 180 degrees (or other values depending on the encoding and code) but can you like check for those codes? I've already seen the same solution somewhere but I don't get it as I'm new to this stuff. ( And teatcher is like 800 years old and doesn't understand it either so no help :confused: )

Yet here you are in a forum full of 800 year olds asking for help. Oh, the irony. :sunglasses:

1 Like

Yes. When you are using one channel to say a few different things, adding something so that 42 is not ambiguous is standard procedure.

And yes, of course… the receiving entity has to look carefully at what's coming in a figure out that N42 is not the same 42 as P42.

This is prolly worth your time

HTH

a7

I don't need to check anything. I would know how to write that....

the goal of school is learning... invest in yourself

No I don't want you to check anything, I just asked is there in the tutorial the explanation on how to check for a specific letter and use the value behind it, or use values of a specific line.

the tutorial includes explanations on how to receive a message

you can parse the message then simply by checking the content of that message

if first character is 's' then 
  // it's a command for the servo
  extract the following number after the letter 
  move the servo
else if if first character is 'm' then 
  // it's a command for the motor
  extract the following number after the letter 
  move the motor at that speed
else ...

Yeah, I'm searching for this solution for a long time, but I can't exactly find the code for that. I've found a tutorial to ceck the first character : "if char[0] == 'm', but I didn't find (not even in serial input basics) where I could find how to extract the last text. Do you what the code is I need to use? Is the char command even good to use, and do I need to input a library beforehand or not?

Sinceriely,

Luca

say you use the Serial Input Basics tutorial to receive a message in the array receivedChar

say your message is in the form mXXXwhere XXX is a number, like m123 and you want to recognise the m and extract the number.

if we assume there is no syntax error possible (don't test for it basically) then you could do

if (receivedChar[0] == 'm') { // we got the 'm' as the first character
  long value = atol(receivedChar+1); // parse an integer (long format) starting at the 2nd character (we skip the first character as this is the 'm')
  Serial.print("I got the 'm' command with value : "); 
  Serial.println(value);
}

That was a fast answer, thank you!
This will help a lot on my project.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.