Pages: [1]   Go Down
Author Topic: Need more pins for project - SPI unused and rewriteable?  (Read 893 times)
0 Members and 1 Guest are viewing this topic.
Ohio
Offline Offline
Jr. Member
**
Karma: 1
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I have an arduino ethernet and I need 8 digital pins.  From the arduino website, it states that pins 10-13 are used for SPI, 4 is used for SD, 2 is used for W5100, and 0 and 1 are used for TX and RX.
This leaves me with 6 pins left. 

My question is, since I am not using SPI, can I set those pins to digital outputs and use them like any other digital pin?  My thought is yes since I am not calling the SPI library.

Matt
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5145
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Ethernet functionality uses SPI. Do you already use the analog inputs? If not, they can be configured as digital I/Os too.
Logged

Ohio
Offline Offline
Jr. Member
**
Karma: 1
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pylon,

Thanks for the heads up.  Looks like I'm keeping the SPI library in my code then :p

I only have 3 of the 6 analog pins used.  How do I go about setting the analog as a digital pin?

Matt
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5145
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do I go about setting the analog as a digital pin?

Just use pinMode() to set the correct direction, then digitalRead() or digitalWrite() to read/write to/from it. It's best use and most portable to use the A0, A1, etc. place holders for the pin numbers.
Logged

Ohio
Offline Offline
Jr. Member
**
Karma: 1
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I can do something like

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

without declaring A0 to be something?

Matt

Logged

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I can do something like

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

without declaring A0 to be something?

Matt

Yes, though I hope you realize that all that will do is enable the internal pullup since you set it as an INPUT. digitalWrite would normally be used for an OUTPUT.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A0 to A5 = D14 to D19, can declare them way also.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Ohio
Offline Offline
Jr. Member
**
Karma: 1
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Long story short, I'm trying to set up the analog pins over ethernet and all im sending is pin number (arduino is server).  I want to control a camera with 2 switches, high on SW1 camera zooms out, high on SW2 camera zooms in.

That being said, I am sending analog pin number to the arduino code.  Does this work?
pinnum is set from an incomming packet.

//sets the pin
pinMode(pinnum+14, 0);
//writes the pin
digitalWrite(pinnum, 1); 

I believe in the pinMode, the 0 means output and in digitalWrite the 1 means high.  Is this correct?

Matt
Logged

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes. Well, except for calling pinnum+14 to set it up and not the same when writing, but I assume that is a typo.

And yes about 0 and 1, but why not just use INPUT,OUTPUT and HIGH,LOW? It doesn't use anymore space in code and makes it easier to read.

Code:
//sets the pin
pinMode(pinNum+14, OUTPUT);
//writes the pin
digitalWrite(pinNum+14, pinState);

That would make a pinNum of 0 control A0, pinNum of 5 would control A5. And pinState would set it to either HIGH or LOW as you designate. Though if the pin is always going to be an OUTPUT, I would put that part in the setup, personally.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm with Retroplayer, use

pinMode(pinX, INPUT); // or OUTPUT, on INPUT_PULLUP to have internal pullup enabled.

http://arduino.cc/en/Reference/PinMode
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Ohio
Offline Offline
Jr. Member
**
Karma: 1
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working with LIFA (Labview for arduino) to communicate over eathernet instead of serial.  I am trying to reuse as much code as possible because I am not proficient with labview or arduino.  Basically, I'm doing this way to stay consistant with the code I already have.

That being said, a 15 byte packet is being sent to the arduino and one of the bytes is output/input.  Instead of creating an if..else.. statement, I am just using the NI code which I know sets the input/output correctly.

Thank you for the help, I got everything working smiley-grin
Matt
Logged

Pages: [1]   Go Up
Jump to: