Pages: [1]   Go Down
Author Topic: If umbrellas or Case for buttons and sensors?  (Read 749 times)
0 Members and 1 Guest are viewing this topic.
Winston-Salem
Offline Offline
Full Member
***
Karma: 0
Posts: 158
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have my application near completion. Much thanks to the many people that have helped me. The icing on the cake will be adding a few things like an all stop button and an unexpected end of travel stop when an optical sensor is tripped. There might be several of these and I am wondering if I should put this at the top of the void loop as a set of IFs like umbrellas or can I use Case for these pins on different pins?
Logged

I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
all stop button

What should happen when the all stop button is pressed?

Quote
an unexpected end of travel stop when an optical sensor is tripped

What should happen when one of these sensors is tripped?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There might be several of these and I am wondering if I should put this at the top of the void loop as a set of IFs like umbrellas or can I use Case for these pins on different pins?

I'm not totally sure what you mean by umbrellas in this context. Perhaps an example? I should point out that you can just return from loop, effectively terminating this particular iteration. Of course loop starts up again a moment later. But at least you skip code inside loop that you might not want to do.

You could implement an "all stop" function by doing something like this:

Code:
void allstop ()
  {
  digitalWrite (1, LOW);   // turn off stuff
  digitalWrite (2, LOW); 
 
  // ... etc. to make things stop

  exit (0);  // don't do anything else
  }

Then when you need to test the "all stop button":

Code:
if (digitalRead (10) == HIGH)
    allstop ();

I should point out that the exit() function will place the sketch into a tight loop, from which you need to reset the processor to recover. But perhaps that is what you want.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3425
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since you're going to be testing for different conditions that all amount to 'oops, better stop', it sounds like a single 'umbrella' if testing all those conditions would be best - I'm not sure how you would do this with case statements. In any event, for a better answer, post your current code.
Logged

Winston-Salem
Offline Offline
Full Member
***
Karma: 0
Posts: 158
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Since my current code is working well, I don’t want to bog you down with that. What I mean by umbrellas is that one IF holds up the entire loop, then a second does the same and so on down the loop until it gets to the meat of the program.

if (digitalRead (10) == HIGH)   // Sensor 1
    {
    allstop ();
    }
// if not
if (digitalRead (11) == HIGH)   // Sensor 2
    {
    allstop ();
    }
if (digitalRead (12) == HIGH)   // All STOP Button
    {
    allstop ();
    }
// if not etc…
Could I somehow read the pins, into var
  switch (var) {
    case 10: // pin 10 is HIGH
      //do something
      break;
    case 11: // pin 11 is HIGH
      //do something
      break;
    case 12: // pin 12 is HIGH
      //do something
      break;
    default: // if all is OK
      // Continue on to rest of loop
  }

>>>or could I do a long if like

if (digitalRead (10) == HIGH && digitalRead (11) == HIGH && etc. )

>>>or will non of this make much difference and not matter?

If I put this in on the top of a step motor loop if might slow down some but maybe not. I don’t know enough about this stuff to tell.
Logged

I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

>>>or could I do a long if like

if (digitalRead (10) == HIGH && digitalRead (11) == HIGH && etc. )

>>>or will non of this make much difference and not matter?


The "long if" saves having to repeat the action, so that would save marginal space. It might look neater as:

Code:
if (digitalRead (10) == HIGH &&
    digitalRead (11) == HIGH &&
    etc. )
  allstop ();

(edit) - see below, the && should be ||. You want to stop if any switch is pressed, not all of them.

If you are testing lots of switches it could be faster to do direct port access. This is because digital pins 10, 11, 12 are all on the same port of the processor port. So for speed (but not necessarily clarity) you could do:

Code:
if (PORTB (_BV(PIND10) | _BV(PIND11) | _BV(PIND12)) != 0)
  allstop ();

What that is doing is a single read of port B and testing if any of the bits corresponding to D10, D11, or D12 are set. So that is a single read and a single test (the compiler will optimize out or'ing together the various bits at compile time).
« Last Edit: May 22, 2011, 05:44:06 pm by Nick Gammon » Logged

Winston-Salem
Offline Offline
Full Member
***
Karma: 0
Posts: 158
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It sounds like this is the direction that I need to go in. Thanks

Now to be specific, I have an Arduino Mega 1560 and I am actually using pins 3, 11, 12, 13 for the main stepper motor and 4, 5, 6, 7, 8, 9 for the LCD. I have arbitrarily picked 22 and 24 for the second stepper motor.

Does the PORTB range of pins start and stop somewhere? What series do you think would work best for me?
Logged

I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it does. Each port handles a maximum of 8 pins, as they are 8-bit ports. Look, for something like a stepper motor controller, doing the digitalRead approach is probably absolutely fine, and then you don't need to look up which pins belong to which ports. The direct read saves a few microseconds, not a great amount.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46214
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (digitalRead (10) == HIGH &&
    digitalRead (11) == HIGH &&
    etc. )
I think you want || there, not &&.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quite right. I amended the earlier post to add an addendum.
Logged

Winston-Salem
Offline Offline
Full Member
***
Karma: 0
Posts: 158
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Look, for something like a stepper motor controller, doing the digitalRead approach is probably absolutely fine, and then you don't need to look up which pins belong to which ports.

Sorry, I don't get what you mean. I still just cut, edit and paste. Where do I look for what? If I use those pins for a steppers, do I just grab the next evaluable 8 pins and define them as a port for sensors?
« Last Edit: May 22, 2011, 06:43:10 pm by Asa Herring » Logged

I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The internal representation of pins in the microcontroller is as three registers, collectively called a 'port'.  Each of these ports corresponds to 8 pins on the microcontroller, usually in sequence; however, the Arduino pin numbers are NOT the same sequence as those on the actual IC.  You can get a marginal speed increase (a couple microseconds) by accessing the pins directly from the registers, known as 'direct port manipulation'.  For your case, this is totally unnecessary - in fact, I only know of two cases where it's necessary: when bit-banging high speed protocols, and when utilizing ISRs that trigger at > 10 KHz. 

If you want to cut, edit, and paste, use digitalRead().

Like this:
Code:
if (digitalRead(pin1) || digitalRead(pin2) || digitalRead(pin3) || digitalRead(pin4)) {
   stop();
}

figure out what pin1, pin2, pin3, and pin4 are, and you'll be fine.
Logged

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 326
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No. The ports are defined on the Mega datasheet. You can find out which ports contain which digital pins by studying the mega schematic http://arduino.cc/en/uploads/Main/arduino-mega-schematic.pdf
Logged

Winston-Salem
Offline Offline
Full Member
***
Karma: 0
Posts: 158
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for the help. I think that the pick up that I will get is small and this is way over my head so I will pass on trying to implement this one. On the other hand, that schematic is very nice and very interesting. Are the boxes different ports? I will google and study this but only for informational use.

Thanks guys! 
Logged

I have an Arduino Mega 2560. I also have a (pH) BNC Sensor Shield,an I2C/SPI/Onewire Shield(I beleive that it an on board clock.)and a Breakout Board Shield, all three are from Andrew Oke. Additionally, I have a Stepping Motor but could certainly buy a Servo Motor instead if it is any better for what I want to do, short term/long term. Trying to read the lable, I guess that it is: Type 57BYG. 12 V/Phase, 20ohm, 0-6A/Phase 1.8 Deg/Step, No. 04052 with a K179 Stepper Driver board attached. I can't confirm that the board is fully funtional

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 326
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
....On the other hand, that schematic is very nice and very interesting. Are the boxes different ports? I will google and study this but only for informational use.

No but the ports are shown in the form PA0, PA1, PB0, PB1, etc. The PA and PB etc represent ports A and B respectively, the numbers are bits on the 8-bit port with bit 0 being the least significant bit and bit 7 the most. Then if you look at the connectors -- for example JP1 you'll see that PC0 is digital pin 37, PC2 is digital pin 35 etc.

Not all bits of all ports are brought out to connectors.

Logged

Pages: [1]   Go Up
Jump to: