Hello,
I am new to arduino programming and the question I have is regarding the use of four components - a grove ultrasonic ranger, a 360 servo and an arduino uno with grove shield.
I am trying to find an example that exhibits how to use an ultrasonic sensor to control the movement of a servo motor, using a switch case?
I appreciate everybody’s time and help,
Thank you
What does the switch/case do?
Control a state machine?
Well this like riding a BMX-bike and after 5 minutes trying to jump a backflip in the half-pipe. Pretty difficult to do with just 5 minutes of practising on a BMX-bike.
There are some basic basics that are important for any kind of code and as soon a you know those basic basics it will become much easier to write code.
Remember the most complex smartphone-app you have ever tried. Multiply the level of difficulty with 100. Now you are on the level of writing code.
This analogon shall show: without the knowledge about the basic basics tinkering around will be frustrating.
Tinking with knowing the basic basics will be a lot of fun.
So my recommendation is
Take a look into this tutorial:
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
best regards Stefan
Hello,
Thank you for your reply!
I have a robotic arm built, it’s base rotates
360 degrees.
I have an ultrasonic sensor on each side of the base ( which is square shaped).
So it has four sides.. & therefore four ultrasonic sensors.
My aim , is to have the robotics arm rotate to whatever side has been triggered..
the robotic arm will then use some other components such as a pixy cam to determine what the object is and whether it will need to be picked up by the arm..
If nothing has been detected on any of the 4 sides of the base, the arm will remain in a fixed position.
If an object has been detected, the arm will rotate to whatever side that the associated ultrasonic sensor that has been trigged on.
After if has carried out its functions and is no longer needed on that side , it will return to its fixed or central position.
The switch case will have 5 cases - 4 for each of the sides( ultrasonic sensors) and 1 default case , which will be to stay in the fixed position. The fixed position has not been determined as I am struggling to find an example of how I will set up the robot to detetct, approach and return to its fixed position.
I appreciate all your time and help.
Thank you very much!
It is hard to know what you are asking. @StefanL38 has some good advice and the following links might also help:
First step in troubleshooting is to break the "system" into sub-systems. how about getting just the 360 servo working with the Uno. Then, get the ultrasonic ranger working with just the Uno. Then get the grove shield working with just the Uno, then combine the shield and the ranger and the Uno, then add the servo last.
Hello,
Thank you for your reply and help.
I have accomplished all of them goals, just started to understand the if statements and then I got lost amongst the entire concept of switch cases...
Post your code so far and we can help debug it. That's a lot easier than re-inventing your wheel and all the discussion and decisions you've already made.
it wont allow me to upload an attachment as I am a new user, is there another way of displaying my code to you?
when you reply, hit enter, then hit the </> button for preformatted text and past it.
#include <Servo.h>
Servo servo
const int sensorMin = 0;
const int sensorMin = 20;
void setup() {
serial.begin(9600);
servo.attach(4);
}
void loop() {
int sensorReading = analogRead(A1);
int range = map (sensorReading,sensorMin,sensorMax,0,1);
switch (range){
case 0:
servo.writeMicroseconds(1400);
servo.writeMicroseconds(1500);
break;
case 1:
servo.writeMicroseconds(0);
break;
delay (1);
}
this is honestly a mix of examples of code, I was struggling to reach my goal, so I narrowed it down to just 2 cases..
First Serial.print(range) so you know if you are mapping the sensor correctly.
It doesn't compile. There is a missing semicolon, closing brace, and sensorMax is not defined.
it is completely OK to be a newcomer. Newcomers are welcome.
learning to write code is different than examining a new smartphone-app.
The main difference is: s smartphone-app is very limited in comparison with writing code.
You should at least read some very basic introductional tutorial to understand the very very basics of a arduino-sketch.
And then there is a special kind of "tinkering" with code to learn how all the functions work.
This special kind of "tinkering" is serial-output.
The serial output makes visible what is going on in the code. And this helps a lot understanding what the code does.
Your code does "open" the serial interface with the command
serial.begin(9600);
The number 9600 is the baudrate. 9600 baud still works but is slow and outdated
@the arduino-IDE-Team: when will you change the baudrate in all the examples to 115200 baud??!
change it to 115200 baud:
serial.begin(115200);
as a start a first version of your code with typos you made corrected and some serial output
#include <Servo.h>
Servo servo;
const int sensorMin = 0;
const int sensorMax = 20;
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
servo.attach(4);
}
void loop() {
int sensorReading = analogRead(A1);
Serial.print( F("sensor read value =") ); // print the fixed text between " "
Serial.println(sensorReading); // print the content of variable
int range = map (sensorReading, sensorMin, sensorMax, 0, 1);
Serial.print( F("mapped value of range=") ); // print the fixed text between " "
Serial.println(range); // print the content of variable
switch (range) {
case 0:
servo.writeMicroseconds(1400);
servo.writeMicroseconds(1500);
break;
case 1:
servo.writeMicroseconds(0);
break;
delay (1);
default:
Serial.print( F("switch-case default range has value=") ); // print the fixed text between " "
Serial.println(range); // print the content of variable
}
}
best regards Stefan
Hello,
I would firstly like to thank you , along with all those who have also replied to my question, I appreciate all of your time and help, so thank you!
I have attempted to run the code , it outlined to me some very important lessons.
I am just wondering if that code is able to run, so that I could have the bot rotate to an angle when the ultrasonic sensor has sensed an ultrasonic object?
Do I just change the statements that are in each case?
Thank you again
it is a nice gesture to say thank you. Anyway
You still haven't yet understood how this forum works.
You should post your attempt how you think that it might could work.
If you go on with such poor postings that only show minimal effort from you
I will just repeat posting the link to
tips how to improve your posting style
best regards Stefan
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.