Go Down

Topic: arduino code help (Read 1 time) previous topic - next topic

mrecengr

Oct 17, 2013, 03:24 am Last Edit: Oct 17, 2013, 04:14 am by mrecengr Reason: 1
Hey guys, i want to see if this code will do what i want it to. each joystick will control a servo's position. The left and right JS are "speed controls" for the continuous rotation servos while the gripper JS is just for position control of servo. i have also added just below my base code another code for controlling speed control of a DC motor. I am wondering how do i combine the two ?!?!
I would really appreciate the help !!

PaulS

Code: [Select]
  pinMode(leftJS, INPUT);              // let Arduino know that this is an input
  pinMode(rightJS, INPUT);  // let Arduino know that this is an input
  pinMode(gripJS, INPUT); // let Arduino know that this is an input

Analog pins are INPUT only, so this is useless.

Code: [Select]
int leftPOS = 90;        // initialize left wheel position variable to 90 degrees
int rightPOS = 90;      // initialize right wheel position variable to 90 degrees
int gripPOS = 90; // intitialize gripper position variable to 90 degrees

These variables are only used in loop(), so they should not be global. The first thing that loop() does is overwrite the values in these variables, so the comments are wrong.

Quote
I am wondering how do i combine the two ?!?!

Any ?!?! text ?!?! editor ?!?! will work ?!?!


zoomkat

For best forum viewing, look at #7 below on how to use the code boxes.

http://forum.arduino.cc/index.php/topic,148850.0.html
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PeterH


Analog pins are INPUT only


That's untrue. They're input by default, so the call to pinMode() is redundant, but the analog pins are NOT input-only.
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Quote
but the analog pins are NOT input-only.

They are so. The analog pin can NOT be used to output anything. Now, get over it!

Grumpy_Mike


alnath


Quote
but the analog pins are NOT input-only.

They are so. The analog pin can NOT be used to output anything. Now, get over it!


As far as I know (checked here ;)  http://arduino.cc/en/Tutorial/AnalogInputPins  ), they can be used as output too, but I wouldn't recommend it, unless you don't plan to use any of them for an analog reading or, if you do, you'll have to be very cautious because of this :
Quote
Details and Caveats
The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pullup resistor will be set, when switched back to an input.

The Atmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.


Then, I'd say, if you don't plan to use ADC and you need more outputs, then it's safe to use them . If you need ADC, and you really need  to use Ax as outputs, then double-check your analog readings and, if you get weird results, you'll know where you should look at first  :smiley-mr-green:

zoomkat

Did both sections of code work ok on their own?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

UKHeliBob


Analog pins are INPUT only, so this is useless.

Paul, I suspect what you meant was that analogue pins cannot be used for analogue (PWM) output, but that is not how your post reads at first sight, hence the comments here to the contrary.

In practice is there any harm in setting the pinMode to OUTPUT for an analogue pin ?  It certainly acts as a reminder in the code as to what the pin is being used for.  Digital pins default to INPUT but it is common practice to set hem explicitly using pinMode.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mrecengr


Did both sections of code work ok on their own?



Yea.

Also thanks for the reply on how to post a code. I was wondering that myself.

PeterH


They are so. The analog pin can NOT be used to output anything. Now, get over it!


The analog pins certainly can be used as digital inputs and outputs.

Code: [Select]

pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);

Now analog input pin zero is outputting a HIGH.
I only provide help via the forum - please do not contact me for private consultancy.

Grumpy_Mike

Quote
The analog pins certainly can be used as digital inputs and outputs.
Yes but then they are not analogue pins they are digital pins.
The point Paul was making is that the analogue function of these pins is for input only. They can be transformed into digital pins but then they are no longer analogue pins.

PeterH


Yes but then they are not analogue pins they are digital pins.


The pin in the corner of the PCB with "A5" printed next to it can be used as a digital I/O pin. It doesn't cease to be A5 just because it's being used for digital I/O. Saying that the analog input pins can't be used for digital I/O doesn't make sense - they plainly can.
I only provide help via the forum - please do not contact me for private consultancy.

Grumpy_Mike

Quote
Saying that the analog input pins can't be used for digital I/O doesn't make sense - they plainly can.

Well I have explained why it does make sense and plainly you can not differentiate a label from a function.

The pin labeled A5 can be used as an analogue input pin. It can also be used as a digital pin but then it is no longer an analog pin but a digital one.
I do not say the physical connector that has the label A5 can not be used as a digital input. I am saying that it is no longer an analogue pin when it is.

The fact that the label now lies to you is down to the fact that there is no smart labeling on the product.

Once A5 is a digital pin it is no longer an analogue pin. The only analogue function this pin has is as an input - there is no analogue output associated with this pin.

Go Up