Pages: [1]   Go Down
Author Topic: arduino code help  (Read 820 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !!

* Arduino_Code (1).txt (2.82 KB - downloaded 33 times.)
« Last Edit: October 16, 2013, 09:14:49 pm by mrecengr » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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:
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 ?!?!

Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am wondering how do i combine the two ?!?!
Try this
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html
Logged

Offline Offline
God Member
*****
Karma: 16
Posts: 601
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink  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
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did both sections of code work ok on their own?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 116
Posts: 4312
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);
Now analog input pin zero is outputting a HIGH.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: