Arduino beginner

You could have an array of six pin numbers, and iterate through it, using the pin numbers to perform a read on that pin.
This kind of stuff gets posted here every other week.
You could also edit your post to put your code between code tags.


in which category does it get posted

Just as AWOL says. You can't read more than one at a time (without extra hardware) but you can read one at a time pretty quickly so you may not need to read all at once.

And then the ADC part of the chip has something that must be worked around. If you switch from one analog pin to another quickly the first read on the new pin won't be accurate so you read it twice and only use the second read as data. You can make about 9 reads per millisecond so getting your 6 analog reads all in one go will take a bit over 1/1000th of a second. Is that fast enough?

But maybe you don't need them all read so close. Maybe you will do other non-ADC things in between and one read per sensor will be good. Even with a state engine you need to control state changes to suit priorities between hardware and what you want it to do.

Find out what you can do before you start planning towards wants and end results, it will save you a lot of code rewriting. Get the pieces all separate and then play with how they might fit and mesh. If you can interleave the processes then your end product may be able to "walk and chew gum at the same time".

just a quick question guys

i uploaded a code to the arduino to test sensors but when i disconnect the arduino from the computer and then connect it again the code rx pin stops flashing and the test doesnt work without uploading the code again. does this mean that everytime i disconnect arduino the code get erased, can someone help

Did you close the serial monitor before you plugged the Arduino back in?

i dont remember , why?

is the serial monitor the reason why??????

If the PC has a connection going as COM and the connect is broken then just plugging the USB back in isn’t going to work. I have done this lots of times when trying things out and kicked my own butt for forgetting the sequence. Many times I have closed the IDE itself down, started it back up and then plugged the UNO in before I figured out what I was doing wrong.

so what do u do to avoid this problem????

Close the serial monitor before disconnecting the Arduino...

thank you

hello everyone,
im writing my code now to control the dc motors based on the feedback received form sensors,
this is my initial code, and whenever i verify it in arduino ide , i get an error message saying
''a function-definition is not allowed here before ‘{’ token

int ultrasoundsignalpins[]={0,1,2,3,4,5}; //6ultrasound signla pins

int ultrasoundtriggerpin=6; // rx pin, the trigger pin

char *sensorstring[]={"left: " , "  frontleft: " , "  front 1: " , "  front 2: " , "  frontright: " , "  Right: "};// (NOT NESSECARY)

float obstacle =constrain(obstacle, 0, 50); 

float notobstacle =constrain(notobstacle, 51, 100);

#include <Servo.h>

Servo M1;
Servo M2;
Servo M3;
Servo M4;

int angle = 0;

void setup()


    for(int i=0; i<6; i++)

pinMode(ultrasoundsignalpins[i],INPUT);//set ultrasoundsignalpins as input}
  pinMode(ultrasoundtriggerpin,OUTPUT);//set rx pin as output
  delay(250);// time to boot up
digitalWrite(ultrasoundtriggerpin, HIGH);//send rx pin high for at least 20microsecs
digitalWrite(ultrasoundtriggerpin, LOW);
pinMode(ultrasoundtriggerpin, INPUT);

M1.attach(5); // attaches the servo M1 on pin 5 to the servo object.
M2.attach(9); // attaches the servo M2 on pin 9 to the servo object.
M3.attach(10); // attaches the servo M3 on pin 10 to the servo object.
M4.attach(11); // attaches the servo M4 on pin 11 to the servo object.


void loop()

for(int i=0; i<6; i++)
			float sensor1 = analogread(ultrasoundsignalpins[0]) * (254.0/1024.0) * 2.54;// reads distances in cm
			float sensor2 = analogread(ultrasoundsignalpins[1]) * (254.0/1024.0) * 2.54;//
			float sensor3 = analogread(ultrasoundsignalpins[2]) * (254.0/1024.0) * 2.54;//
			float sensor4 = analogread(ultrasoundsignalpins[3]) * (254.0/1024.0) * 2.54;//
			float sensor5 = analogread(ultrasoundsignalpins[4]) * (254.0/1024.0) * 2.54;//
			float sensor6 = analogread(ultrasoundsignalpins[5]) * (254.0/1024.0) * 2.54;//

	        	do    {
				analogread(ultrasoundsignalpins[i]);//reads sensors first

			while (sensor1 == obstacle; sensor2 == notobstacle, sensor3 == notobstacle, sensor4 == notobstacle, sensor5 == notobstacle, sensor6 == notobstacle)
			//then run OP I	first command only
			for(angle = 0; angle < 180; angle += 1) // goes from 0 degrees to 180 degrees
			{ // in steps of 1 degree
			M1.write(angle); // tell servo to go to position in variable 'angle'
			delay(20); // waits 20ms between servo commands
			for(angle = 180; angle >= 1; angle -= 1) // goes from 180 degrees to 0 degrees
			M1.write(angle); // tell servo to go to position in variable 'pos'
			delay(20); // waits 20ms between servo commands


Can someone help me fix it pls
this is the code , and when the message appears, the pointer stops after Void loop() { <=(it highlight this bracket)

analogread != analogRead

Your “while” condition needs a rework.

If you’d tried the auto-format tool in the IDE before you posted your code (hint), it would have told you part of the problem with your code.

Edit: I took a longer look through.
It looks like you’ve got some pretty major misconceptions about how things work in C (e.g. “constrain” must be called every time you want to constrain a value; it doesn’t automagically bind to a variable and operate on it whenever you reference that value).
When I said earlier to iterate through an array of pin numbers, I didn’t mean to set up a for loop, then execute the exact same code for each of the array members explicity - that’s why we have for loops.
Your “do…while” looks like you don’t understand how to combine conditions, and doesn’t make any sense.

Here,      for(angle = 0; angle < 180; angle += 1) // goes from 0 degrees to 180 degree the code does not match the comment. This is no big deal, because the compiler ignores the comment, but it is best to try to get the comment to describe the action, or you may as well miss it out altogether.

I’d recommend you take a few days going through some tutorial and examples, and working through them.

Just a quick comment, since you mentioned you were using Sabertooth motor drivers.
Be sure to use servo.attach(pin, 1000, 2000); instead of just servo.attach(pin);

If you use the 1000, 2000 variation, you can use angles from 0 to 180 like a normal servo.
If you don’t it’s more like 40 to 140 (the Arduino uses a nonstandard, wide range for servo – using
the three argument version fixes that).

float obstacle =constrain(obstacle, 0, 50); 

float notobstacle =constrain(notobstacle, 51, 100);

Please explain what you think this code is doing. It isn’t, but I’d like to hear what you think it is doing.

i was trying to define a variable called obstacle which have a number range between 0 and 50, to be more clear with you i wanted to say that if eg sensor1 read a value between 0 and 50 then that is an obstacle.

so i was thinking that creating defining an obstacle using this constrain and equating to the sensor was gona work but apparently it doesnt,

could anyone give me a more easier way to do this, please as if I've spent the last 5 hours trying to figure how to debug this problem and now im frustrated, i would really appreciate your help

Try spending time finding out how C works instead of why it doesn't fit your ideas of how it works.

Your PC has an FPU, Arduino does not. Don't use floating point unless you really need to.