stepper motor

Active today
Viewed 17 times

I am working through the ‘Ultimate Starter Kit” for my Raspberry Pi 3+. In Chapter 16 the tutorial shows how to work with a stepper motor.

Note: The program works per the tutorial –

I just cannot understand how the following line is working:

GPIO.output(motorPins[i],((CCWStep[j] == 1<<i) and GPIO.HIGH or GPIO.LOW))

motorPins is a four-element array with a hex number in each element.

How is this line evaluated?

 New contributor
  • That statement is meaningless without context; is a general programming question not specific to the Pi. If you want to learn programming try to avoid complex statements like that (which are poor practice) and try to break down into each component. – Milliways 3 hours ago


GPIO.output(motorPins[i],((CCWStep[j] == 1<<i) and GPIO.HIGH or GPIO.LOW))

GPIO.output is a function which takes two parameters.

The first motorPins[i] specifies the GPIO to act upon. In this case it is entry i in the array motorPins.

The second ((CCWStep[j] == 1<<i) and GPIO.HIGH or GPIO.LOW) specifies the level to assign to the GPIO.

If the expression evaluates to True the GPIO will be set high otherwise it will be set low.

I’m not going to try to work through that expression as it depends on CCWStep.

I would say it is (in my opinion) terrible code and a good example of code you should never write yourself.

  • Thank You. I understand the part you explained and am grateful I am not the only one thinking the code is obtuse. – Goose23 2 hours ago

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: