Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

determine if an integer is evenly divisible by another integ
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

determine if an integer is evenly divisible by another integ

by Steve_3000 on Sat May 23, 2020 4:44 pm

In CircuitPython I would like to determine if an integer vbig is evenly divisible by another integer vsmall. So that if it is, I can make something happen, perhaps with a simple IF statement. For instance, getting the red LED sitting on D13 to light up, while I figure out what comes next.

This would be on a Feather M4 Express with CircuitPython 5.3. Thank you for any suggestions or links. Math is not my strong suit. Nor arithmetic; nor coding. ;-)

Steve_3000
 
Posts: 28
Joined: Wed Oct 10, 2018 2:39 pm

Re: determine if an integer is evenly divisible by another i

by kevinjwalters on Sat May 23, 2020 8:14 pm

The two main ingredients here are:


So you want something like this. I've put two different styles of progamming for setting the led value.

Code: Select all | TOGGLE FULL SIZE
import time
import board
from digitalio import DigitalInOut, Direction

led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

vbig = 240  # pennies in a pound
vsmall = 12  # pennies in a shilling

if vbig % vsmall == 0:
    led.value = True
else:
    led.value = False

time.sleep(2)
vsmall += 1

# The boolean result from the comparison can be directly assigned
# to do the same thing on a single line
led.value = vbig % vsmall == 0

time.sleep(2)
vsmall -= 1

led.value = vbig % vsmall == 0

while True:  # pause to stop interpreter finishing and reseting anything
    pass

kevinjwalters
 
Posts: 634
Joined: Sun Oct 01, 2017 3:15 pm

Re: determine if an integer is evenly divisible by another i

by Steve_3000 on Wed May 27, 2020 1:15 pm

Kevin, thank you for explaining and providing examples for the modulo operator. Works just as needed.

I have a Feather M4 Express with CP 5.3. Sitting on top of it is a DS3231 Precision RTC FeatherWing, product ID 3028. As part of increasing my infinitesimal understanding of CP (as I work toward a small project) I wanted to apply the modulo ability to cause Serial to print a variable once every five-ish seconds. I wanted to do this because when I tried adding "time.sleep(5)" to my code I realized it paused the entire code, not just the thing I wanted to briefly pause. I am still getting used to the idea that for the M4 to work the entire code is within a loop running constantly and should not be obstructed.

But my "if vwhat = True" code at line 29 fails with a syntax error. I have tried many different variations but have not hit on the correct syntax. I am sorry, but I am asking for someone to give me the code I need. I need the fish now, but any fishing hints would also be appreciated. I have appended my code to this post, most of it hijacked from Welcome to CP, CP Essentials, and Kevin's kind reply to my previous post.

Code: Select all | TOGGLE FULL SIZE
# CircuitPython IO demo #1 - General Purpose I/O
import time
import board
import busio
i2c = busio.I2C(board.SCL, board.SDA)
import adafruit_ds3231
from digitalio import DigitalInOut, Direction, Pull
ds3231 = adafruit_ds3231.DS3231(i2c)
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

# For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express, Itsy M4 Express
# switch = DigitalInOut(board.D2)
switch = DigitalInOut(board.D5)  # For Feather M0 Express, Feather M4 Express
# switch = DigitalInOut(board.D7)  # For Circuit Playground Express
switch.direction = Direction.INPUT
switch.pull = Pull.UP

while True:
    # We could also do "led.value = not switch.value"!
    if switch.value:
        led.value = False
    else:
        led.value = True
    time.sleep(0.01)  # debounce delay
    current = ds3231.datetime
    vsec = current[5]
    vwhat = vsec % 5 == 0
    if vwhat = True
        print(current)
#    print(vwhat)
#    print('The current time is: {}/{}/{} {:02}:{:02}:{:02}'.format(current.tm_mon, current.tm_mday, current.tm_year, current.tm_hour, current.tm_min, current.tm_sec))
#    time.sleep(5)
#    print (vsec)

Steve_3000
 
Posts: 28
Joined: Wed Oct 10, 2018 2:39 pm

Re: determine if an integer is evenly divisible by another i

by kevinjwalters on Wed May 27, 2020 1:33 pm

Computer languages have to differentiate between a test of equivalence and assignment, Python in common with many other languages uses == and =, respectively. So you want:

Code: Select all | TOGGLE FULL SIZE
    if vwhat == True:
        print(current)


but for a boolean it's more natural not to do a comparison and just evaluate the (boolean) variable:

Code: Select all | TOGGLE FULL SIZE
 
    if vwhat:
        print(current)


Sometimes there's an additional variety of test to check if the variables have not just the same value but are identical in some way like being represented by the same underlying data structure. Python has is for this. I don't do JavaScript but, for comparison, I believe it's === there, see https://stackoverflow.com/questions/544 ... s-equality

kevinjwalters
 
Posts: 634
Joined: Sun Oct 01, 2017 3:15 pm

Re: determine if an integer is evenly divisible by another i

by Steve_3000 on Sun Jun 07, 2020 12:39 pm

Thank you for the explanations of equivalence and assignment. My Code Is Working. :-)

Steve_3000
 
Posts: 28
Joined: Wed Oct 10, 2018 2:39 pm

Please be positive and constructive with your questions and comments.