Arduino Forum

Using Arduino => General Electronics => Topic started by: SouthernAtHeart on Feb 22, 2011, 06:40 am

Title: tx & rx pins
Post by: SouthernAtHeart on Feb 22, 2011, 06:40 am
Can I use digital pins 0 and 1 just like any other of the digital pins?  Does it just mean that I wouldn't be able to use the serial port back to my pc to get serial.print commands?

what about analog pins, how do I use them for simple digital pins, or can I?
Title: Re: tx & rx pins
Post by: Coding Badly on Feb 22, 2011, 06:48 am
Can I use digital pins 0 and 1 just like any other of the digital pins?


Yup.

Quote
Does it just mean that I wouldn't be able to use the serial port back to my pc to get serial.print commands?


Yup.  You may have problems uploading the next Sketch.  Holding reset until the upload starts should get you through.

Quote
what about analog pins, how do I use them for simple digital pins, or can I?


They are accessed in exactly the same fashion (pinMode, digitalRead, digitalWrite).  The pin numbering starts at one more than the last digital pin.  For example...

http://arduino.cc/en/Main/ArduinoBoardUno

The last digital pin is "13".  Analog pin "A0" is also digital pin "14".  Analog pin "A1" is also digital pin "15".  Etcetera.

Title: Re: tx & rx pins
Post by: CrossRoads on Feb 22, 2011, 06:51 am
D0 D1 are digital pins like any other.
They can't really do 2 things at once tho, so you do lose the Rx Tx cabability.
For Tx back to the PC, the stuff on the pin still gets sent over the Tx line, may appear as gibberish on the monitor.

A0-A5 can be used as D14-D19.
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 22, 2011, 06:56 am

Can I use digital pins 0 and 1 just like any other of the digital pins?


Yup.

Quote
Does it just mean that I wouldn't be able to use the serial port back to my pc to get serial.print commands?


Yup.  You may have problems uploading the next Sketch.  Holding reset until the upload starts should get you through.

Quote
what about analog pins, how do I use them for simple digital pins, or can I?


They are accessed in exactly the same fashion (pinMode, digitalRead, digitalWrite).  The pin numbering starts at one more than the last digital pin.  For example...

http://arduino.cc/en/Main/ArduinoBoardUno

The last digital pin is "13".  Analog pin "A0" is also digital pin "14".  Analog pin "A1" is also digital pin "15".  Etcetera.



GREAT!
So there's actually 20 digital pins available on the Uno, then.  I thought someone said 19.
My first project is coming up with 19 pins.  Too bad it's not 18, then the serial would still be available.  I won't need it once I'm done coding, though.
I could use a shiftreg on my LCD, it looks pretty simple.  It looks easier than analoging a 4x3 keypad down to 1 pin.
Title: Re: tx & rx pins
Post by: CrossRoads on Feb 22, 2011, 07:03 am
D0-D13, and A0-A5 (D14-D19). I guess that does make 20.

keypad down to 1 pin - there have been plenty of discussions on that too, using an analog input and a bunch of resistors.
Folks have shown successful examples.
Title: Re: tx & rx pins
Post by: Coding Badly on Feb 22, 2011, 07:05 am
If a lack of Serial becomes too much of a burden and you have one unused pin, let me know.  I may be able to help.
Title: Re: tx & rx pins
Post by: graynomad on Feb 22, 2011, 07:38 am
Quote
My first project is coming up with 19 pins.  Too bad it's not 18,

Post the requirements or a schematic, maybe someone can save you a pin or two.

______
Rob
Title: Re: tx & rx pins
Post by: andywatson on Feb 22, 2011, 04:10 pm
What about this multiplexer:  http://www.robotsimple.com/sparkfun_Analog_Digital_MUX_Breakout

That would free up a lot of pins on your Uno.
Title: Re: tx & rx pins
Post by: robtillaart on Feb 22, 2011, 04:22 pm
nice mux andy

and what about the good old shift registers ? 74HC595 &  74HC165 ?
Title: Re: tx & rx pins
Post by: CrossRoads on Feb 22, 2011, 05:14 pm
Once SouthernAtHeart comes back we can see what he's doing with all these pins.
In other posts he has asked about driving motors and keypad interfacing.
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 23, 2011, 12:48 am

Once SouthernAtHeart comes back we can see what he's doing with all these pins.
In other posts he has asked about driving motors and keypad interfacing.


I'll fill you in here on my project, later today, if you promise not to laugh!
Title: Re: tx & rx pins
Post by: CrossRoads on Feb 23, 2011, 02:11 am
well you've mentioned a wide track 4' robot, motors, keypad for # of coffee cups, what else are you adding?
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 23, 2011, 04:35 am
It's the coffee lovers' automaton.
Your coffee maker sets in the kitchen cabinet, at counter top level.  A few knocks on the door (user customizable knocks) brings the self grinding machine out, opening the doors on it's way.  the dovetailed platform mounted on undermount drawer glides will be likely driven by Sparkfun's 100:1 gearmotor.  (Although, a stepper motor controled by a rotary encoder option moving the platform precisely how you want it sounds 'nice'.)
  Then you are looking at a nice anodized aluminum enclosure housing a 4x3 keypad and an LCD display giving helpful tips to enter the number of cups of coffee you think you need to get you going.  (the user will also have the option to 'bring on the coffee" by the keypad, if they're not up to knocking).  Upon pressing start, your Asco NEMA rated water valve opens, and begins filling your coffee pot with the designated amount of water through 1/4" tubing routed from the undersink water filter system.  The tubing is connected to the underside of the platform not unlike that ribbon connected to your printhead that allows it to move back and forth.  The water is being monitored by seeedstudio's flow sensor.  The # of pulses for each water setting (2,4,6,8,10) is stored in EEPROM, so it is user configurable using a secret keycode to enter the setup menu.  The setup menu will also allow you to choose options:  Enable knock sensor, enable photocell (if it's a dark early morning, the LCD screen in standby mode may show some ZZZZZZZZZZZZZZ's on the screen).  Another option will be to enable audio.  Ever hear the melodic tone played by one of these expensive washing machines when it's done?  Not sure until I experiment, if I get that nice of a sound from 1 I/O pin, to give a little 'cupboard door open' and 'cupboard door closed' tone.
  So, that about maxes out my Uno's pins.  (I'm wondering how far into this paragraph I maxed out it's 2K of ram?)  I'll be asking for help on that after my Uno comes in 2 days.
   I can't think of any other sensors to incorporate, unless it's SparkFun's Optical Detector http://www.sparkfun.com/products/246 (http://www.sparkfun.com/products/246) to monitor when to turn the gearmotor off upon opening and closing of the coffee platform.  That might be easier than trying to wire up 2 unseen limit switches ...I'm still pondering that area of the project...
  It's kind of interesting, I'd never even heard of Arduino a month ago!
    I'll probably be coding, decoding, and recoding for awhile.  I didn't know the first thing about C++, but this forum has been REALLY helpful and enjoyable.
  So, that's my project. :)

Title: Re: tx & rx pins
Post by: graynomad on Feb 23, 2011, 06:13 am
Quote
I can't think of any other sensors to incorporate,

Heck there's a lot of sensors around, I'm sure you can add a few, for example isn't it important to have the coffee at just the right temperature. :)

Sounds real interesting and a fun project. I lost track of the pins required, when you get a schematic (even a rough one) give us a look and we may be able to trim things a bit.

______
Rob
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 23, 2011, 06:15 am
I'd be happy to post my code with offers on improvement!  If that's not imposing on anyone...
If it's split up into tabs, is there any way to post it here?  Or just zip it up and attach?
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 23, 2011, 06:19 am












Pins   Description   Arduino Pin#
2   Serial to PC (debugging)   0,1
2   Shift LCD               12,13
1   Motor (on/off)            A4 (18)
1   Motor Relay 2P2T (reverse)   A3 (17)
1   Optical Detector           A2 (16)
1   I/O Knock sensor        A1 (15)
1   I/O Photocell              A0 (14)
1   Solenoid (water valve)   10
1   Flow Sensor                  2
7   Keypad                         3,4,5,6,7,8,9
1   I/O for Audio                  11
19   Total   

This is my pin line up
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 23, 2011, 08:22 am
Here's my rough draft.  Ugh.  If anyone would like to re-model it into real code I would be VERY happy.  I've only ever coded in basic before a week ago, so this probably looks 'basic'.
All I worked on was the actual water flow part of the code.  I figured I should get that up and running first, before adding any frills like audio feedback or light sensing photocells...



attatchment removed, it was terrible!
Title: Re: tx & rx pins
Post by: graynomad on Feb 23, 2011, 10:57 am
At a quick glance the code looks reasonable but I doubt you have compiled it yet.

Quote
so this probably looks 'basic'.

Yep

//Load Settings from eeprom
LoadSettings;

try

LoadSettings();

I'll look a bit more later.

______
Rob

Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 23, 2011, 04:09 pm
I think my code needs a LOT of help!  If anyone can tell what I'm trying to have it do and can fix it up a bit in a few minutes, they'll save me hours...
Title: Can someone critique this please
Post by: SouthernAtHeart on Feb 23, 2011, 09:52 pm
My first attempt was pretty bad, so I deleted it.  This seems a lot more concise.  It even compiles!
Could someone please look it over and tell me if it will work? 
My Uno will be here tomorrow.
Title: Re: tx & rx pins
Post by: graynomad on Feb 24, 2011, 06:10 am
For someone who's never written any C I think you've done well.

I have not looked at the overall logic, just the syntax and local issues.


In Confirm_Pour()

Code: [Select]
do
  {
    if (key == '*') PourWater;  //turn on the water
    if (key == '#')
    {
      srlcd.clear();
      srlcd.print("Action canceled");
      delay (5000); //wait for user to read message
      return;  //go back to the main loop routine
    }
  }
  while(key = NO_KEY);   


Two problems here. "key" is never updated here so there's no need for a while loop.

  while(key = NO_KEY);   

Assignment instead of comparison.

PourWater =>  PourWater()

Code: [Select]
   while (key != NO_KEY)
    {
      key = keypad.getKey();
      //just waiting for a key press here to start water flow...
    }

Shouldn't these be "key == NO_KEY"?

Code: [Select]
void CountPulse()
{
   FlowSensorCount++;  //increases the count from the interupt pin 2
}

Is the interrupt signal debounced?

in UserSettings()

while (key != '1' || key != '0'); =>  while (key != '1' && key != '0');

I'll keep browsing.

______
Rob
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 24, 2011, 06:42 am
Thanks!  I'm trying to take in everything you said, I got most of it.
What about the Confirm_Pour() function, I will need the while statement or some kind of loop statement, won't I.  It's to give the user the option to cancel, after they enter a number.
How does this look:
Code: [Select]

void Confirm_Pour()
{
  srlcd.clear(); // Clear display, set cursor position to zero
  srlcd.print(key);  //LCD display the number they selected
  srlcd.print(" cups"); //plus cups
  srlcd.setCursor(0, 1); // Sets cursor position to the 2nd line
  srlcd.print("Press '*' to start or # to cancel"); //This needs to scroll across the 2nd line somehow so it can all be read
  do
  {
    key = keypad.getKey();
    if (key == '*') PourWater();  //turn on the water
    if (key == '#')
    {
      srlcd.clear();
      srlcd.print("Action canceled");
      delay (5000); //wait for user to read message
      return;  //go back to the main loop routine
    }
  }
  while(key == NO_KEY);   
}


Quote
while(key = NO_KEY); 

fixed

Quote
PourWater =>  PourWater()

fixed

Quote

Code: [Select]
   while (key != NO_KEY)
    {
      key = keypad.getKey();
      //just waiting for a key press here to start water flow...
    }

Shouldn't these be "key == NO_KEY"?

yes, fixed


Quote
Code: [Select]
void CountPulse()
{
   FlowSensorCount++;  //increases the count from the interupt pin 2
}

Is the interrupt signal debounced?

I reckon it doesn't need to be, does it?  It's an interrupt signal on pin 2 coming from this flow sensor (hall effect):
http://www.seeedstudio.com/depot/datasheet/water%20flow%20sensor%20datasheet.pdf (http://www.seeedstudio.com/depot/datasheet/water%20flow%20sensor%20datasheet.pdf)


Quote
while (key != '1' || key != '0'); =>  while (key != '1' && key != '0');

Are you sure?  That's actually what I had first, but then I thought, key will NEVER be 1 and 0 at the same time so the while statement will never end...  I'll go back and re-reason through it.

Quote
I'll keep browsing.

THANKS!
Title: Re: tx & rx pins
Post by: graynomad on Feb 24, 2011, 07:23 am
I said

Quote
Two problems here. "key" is never updated here so there's no need for a while loop.


because at the time there was no

Code: [Select]
key = keypad.getKey();
inside the loop. Now there is so yes you need the loop.

The flow sensor data sheet doesn't spec the exact nature of the output signal. However for the moment I think it's safe to assume it's clean, you can always add debouncing later if needed.

Quote
That's actually what I had first, but then I thought, key will NEVER be 1 and 0

Maybe, I always have trouble getting my head around these multiple !=, ==, !!, && constructs. So how about we remove the ambiguity and do this.

Code: [Select]
do
    {
      key = keypad.getKey();  //check for key event
      if (key = '0') return;       //action canceled
      if (key = '1')
      {
        for (int i=0; i < 5; i++){
          MySettings.mySensVals[i] = 0;  //reset sensor values
        }
        EEPROM_writeAnything(0, MySettings);  //save the settings
        srlcd.print("Completed!");
        delay(4000);  //wait for user to read message
        return;
      }
    }
  while (1);


______
Rob
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 24, 2011, 07:26 am
Quote
Code: [Select]
do
    {
      key = keypad.getKey();  //check for key event
      if (key = '0') return;       //action canceled
      if (key = '1')
      {
        for (int i=0; i < 5; i++){
          MySettings.mySensVals[i] = 0;  //reset sensor values
        }
        EEPROM_writeAnything(0, MySettings);  //save the settings
        srlcd.print("Completed!");
        delay(4000);  //wait for user to read message
        return;
      }
    }
  while (1);


I like it.  I can read it.  I looked at the conglomeration of NOTs, ORs, AND's, and WHILEs, and still couldn't get it.
I can follow this!
Title: Re: tx & rx pins
Post by: graynomad on Feb 24, 2011, 07:42 am
Yes it's important for code to be clear to a human as well as correct for the computer. And just to show that I don't practice what I preach, here's a line from the app I'm writing now.

Code: [Select]
Serial.print (isprint(((*(start + i)) & 0xFF)) ? (*(start + i)& 0xFF) : '.', BYTE);

Quick, you have 5 seconds to tell me what this does :)

______
Rob
Title: Re: tx & rx pins
Post by: SouthernAtHeart on Feb 24, 2011, 07:44 am

Yes it's important for code to be clear to a human as well as correct for the computer. And just to show that I don't practice what I preach, here's a line from the app I'm writing now.

Code: [Select]
Serial.print (isprint(((*(start + i)) & 0xFF)) ? (*(start + i)& 0xFF) : '.', BYTE);

Quick, you have 5 seconds to tell me what this does :)

______
Rob


...errrr, ummmm...
Title: Re: tx & rx pins
Post by: balajitenetchat on Mar 05, 2011, 10:15 am
If you want to use rx tx pins, why breaking head when there is software serial available. Anyway good effort.
Title: Re: tx & rx pins
Post by: robtillaart on Mar 05, 2011, 02:11 pm
@graynoman
writes printable chars in a hex(?)dump from some start address  ...

You can sent it to - http://www.ioccc.org/ - but I guess it is to readable :)
Title: Re: tx & rx pins
Post by: graynomad on Mar 05, 2011, 02:38 pm
He he, yes it's the bit at the end of a HEX dump that prints a character or a dot if the value is non-printable.

I remember those obfuscated C contests, code in the shape of an hour glass etc. I'll have to see if I can make my code harder to understand in future :)

I also remember when C++ first came out, I saw a program that was something like this

Code: [Select]
main () {
   int i;
}


The entire program was written in the constructor for an integer or something.

______
Rob
Title: Re: tx & rx pins
Post by: robtillaart on Mar 05, 2011, 02:40 pm
The nicest I recall was

#include "/dev/tty"

so you could type in the whole application (no errors allowed) from your head into the command console. never succeeded