Go Down

Topic: If umbrellas or Case for buttons and sensors? (Read 1 time) previous topic - next topic

Asa Herring

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

Coding Badly

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?

Nick Gammon


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: [Select]
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: [Select]
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

wildbill

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.

Asa Herring

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

Nick Gammon

#5
May 22, 2011, 10:46 pm Last Edit: May 23, 2011, 12:44 am by Nick Gammon Reason: 1

>>>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: [Select]
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: [Select]
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).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Asa Herring

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

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Code: [Select]
if (digitalRead (10) == HIGH &&
    digitalRead (11) == HIGH &&
    etc. )

I think you want || there, not &&.

Nick Gammon

Quite right. I amended the earlier post to add an addendum.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Asa Herring

#10
May 23, 2011, 01:39 am Last Edit: May 23, 2011, 01:43 am by Asa Herring Reason: 1
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?
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

Aeturnalus

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: [Select]

if (digitalRead(pin1) || digitalRead(pin2) || digitalRead(pin3) || digitalRead(pin4)) {
   stop();
}


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

RoyK

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

Asa Herring

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

RoyK

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.


Go Up