hydronics2 / 2019-easy-bee-counter

Bee-counter-easy
Other
44 stars 12 forks source link

Translate into micropython #10

Closed jhn4 closed 4 years ago

jhn4 commented 4 years ago

Hello, i tried to translate your code into micropython, i'm not a vey experienced coder, I dont get it to work really. I'm using the Fipy micro controller from pycom, i don't know if you are familiar with it? This is the code that i almost copied from your arduino code, it's the same expect how things are written in micropython. The issue i got is that i don't get anything from the sensors, they're always 0. That makes me think that the issue is in the serial connection.

from machine import Pin
import utime
# How many shift register chips are daisy-chained.
NUMBER_OF_SHIFT_CHIPS = 3

# Width of data (how many ext lines)
DATA_WIDTH = NUMBER_OF_SHIFT_CHIPS * 8

# Width of pulse to trigger the shift register to read and latch.
PULSE_WIDTH_USEC = 5

# Optional delay between shift register reads.
POLL_DELAY_MSEC = 1

pinValues = 0
oldPinValues = 0

def read_shift_regs():
    bitVal = 0
    bytesVal = 0
    ploadPin.value(0)
    utime.sleep_us(PULSE_WIDTH_USEC)
    ploadPin.value(1)

    for i in range(DATA_WIDTH):

        bitVal = dataPin.value()
        # Set the corresponding bit in bytesVal.
        print(i,bitVal,bytesVal)
        bytesVal |= (bitVal << ((DATA_WIDTH-1) - i))
        clockPin.value(1)
        utime.sleep_us(PULSE_WIDTH_USEC)
        clockPin.value(0)
    return bytesVal

def display_pin_values():
    print("Pin states: ")
    for i in range(DATA_WIDTH):
        if(pinValues >> i & 1):
            print(" Pin-" ,i ,": HIGH")
        else:
            print(" Pin-" ,i ,": LOW")
    print("\r\n")

#Set up
ploadPin = Pin('P2', mode=Pin.OUT) #Blue
clockPin = Pin('P14', mode=Pin.OUT) #Purple
dataPin = Pin('P4', mode=Pin.IN) #Yellow

clockPin.value(0)
ploadPin.value(1)
pinValues = read_shift_regs()
display_pin_values()
oldPinValues = pinValues
while True:
    pinValues = read_shift_regs()
    if(pinValues != oldPinValues):
        print("Pin value change detected*\r\n")
        print(pinValues)
        display_pin_values()
        oldPinValues = pinValues
    utime.sleep_ms(POLL_DELAY_MSEC)
hydronics2 commented 4 years ago

did you start with the example code... test_shift_registers? https://github.com/hydronics2/2019-easy-bee-counter/blob/master/arduino/test_shift_registers/test_shift_registers.ino

yeah... it looks like Adafruit wrote a little library for the shift out register 74hc595... It might be helpful to start from this code... buy yeah writing your own library as a beginner programmer can be a bit daunting! Maybe post to the arduino forum and see if anyone wants to give you some pointers? There are lots of helpful people there...

Try posting your question here: https://forums.adafruit.com/viewforum.php?f=60 If you copy me on your post/answer I'll try to help out too.

On Thu, Jun 11, 2020 at 2:26 AM jhn4 notifications@github.com wrote:

Hello, i tried to translate your code into micropython, i'm not a vey experienced coder, I dont get it to work really. I'm using the Fipy micro controller from pycom, i don't know if you are familiar with it? This is the code that i almost copied from your arduino code, it's the same expect how things are written in micropython. The issue i got is that i don't get anything from the sensors, they're always 0. That makes me think that the issue is in the serial connection.

from machine import Pin import utime

How many shift register chips are daisy-chained.

NUMBER_OF_SHIFT_CHIPS = 3

Width of data (how many ext lines)

DATA_WIDTH = NUMBER_OF_SHIFT_CHIPS * 8

Width of pulse to trigger the shift register to read and latch.

PULSE_WIDTH_USEC = 5

Optional delay between shift register reads.

POLL_DELAY_MSEC = 1

pinValues = 0 oldPinValues = 0

def read_shift_regs(): bitVal = 0 bytesVal = 0 ploadPin.value(0) utime.sleep_us(PULSE_WIDTH_USEC) ploadPin.value(1)

for i in range(DATA_WIDTH):

    bitVal = dataPin.value()
    # Set the corresponding bit in bytesVal.
    print(i,bitVal,bytesVal)
    bytesVal |= (bitVal << ((DATA_WIDTH-1) - i))
    clockPin.value(1)
    utime.sleep_us(PULSE_WIDTH_USEC)
    clockPin.value(0)
return bytesVal

def display_pin_values(): print("Pin states: ") for i in range(DATA_WIDTH): if(pinValues >> i & 1): print(" Pin-" ,i ,": HIGH") else: print(" Pin-" ,i ,": LOW") print("\r\n")

Set up

ploadPin = Pin('P2', mode=Pin.OUT) #Blue clockPin = Pin('P14', mode=Pin.OUT) #Purple dataPin = Pin('P4', mode=Pin.IN) #Yellow

clockPin.value(0) ploadPin.value(1) pinValues = read_shift_regs() display_pin_values() oldPinValues = pinValues while True: pinValues = read_shift_regs() if(pinValues != oldPinValues): print("Pin value change detected*\r\n") print(pinValues) display_pin_values() oldPinValues = pinValues utime.sleep_ms(POLL_DELAY_MSEC)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hydronics2/2019-easy-bee-counter/issues/10, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA3MOPB7IRYLNQO5UMNGE3DRWCPN5ANCNFSM4N3HPROQ .

hydronics2 commented 4 years ago

here's a link to the 74hc595 library (shift-out) shift register.. https://github.com/adafruit/Adafruit_CircuitPython_74HC595 We need a library for the 74hc165 (shift-in) shift register

On Thu, Jun 11, 2020 at 11:06 AM Thomas Hudson hydronics@gmail.com wrote:

did you start with the example code... test_shift_registers?

https://github.com/hydronics2/2019-easy-bee-counter/blob/master/arduino/test_shift_registers/test_shift_registers.ino

yeah... it looks like Adafruit wrote a little library for the shift out register 74hc595... It might be helpful to start from this code... buy yeah writing your own library as a beginner programmer can be a bit daunting! Maybe post to the arduino forum and see if anyone wants to give you some pointers? There are lots of helpful people there...

Try posting your question here: https://forums.adafruit.com/viewforum.php?f=60 If you copy me on your post/answer I'll try to help out too.

On Thu, Jun 11, 2020 at 2:26 AM jhn4 notifications@github.com wrote:

Hello, i tried to translate your code into micropython, i'm not a vey experienced coder, I dont get it to work really. I'm using the Fipy micro controller from pycom, i don't know if you are familiar with it? This is the code that i almost copied from your arduino code, it's the same expect how things are written in micropython. The issue i got is that i don't get anything from the sensors, they're always 0. That makes me think that the issue is in the serial connection.

from machine import Pin import utime

How many shift register chips are daisy-chained.

NUMBER_OF_SHIFT_CHIPS = 3

Width of data (how many ext lines)

DATA_WIDTH = NUMBER_OF_SHIFT_CHIPS * 8

Width of pulse to trigger the shift register to read and latch.

PULSE_WIDTH_USEC = 5

Optional delay between shift register reads.

POLL_DELAY_MSEC = 1

pinValues = 0 oldPinValues = 0

def read_shift_regs(): bitVal = 0 bytesVal = 0 ploadPin.value(0) utime.sleep_us(PULSE_WIDTH_USEC) ploadPin.value(1)

for i in range(DATA_WIDTH):

    bitVal = dataPin.value()
    # Set the corresponding bit in bytesVal.
    print(i,bitVal,bytesVal)
    bytesVal |= (bitVal << ((DATA_WIDTH-1) - i))
    clockPin.value(1)
    utime.sleep_us(PULSE_WIDTH_USEC)
    clockPin.value(0)
return bytesVal

def display_pin_values(): print("Pin states: ") for i in range(DATA_WIDTH): if(pinValues >> i & 1): print(" Pin-" ,i ,": HIGH") else: print(" Pin-" ,i ,": LOW") print("\r\n")

Set up

ploadPin = Pin('P2', mode=Pin.OUT) #Blue clockPin = Pin('P14', mode=Pin.OUT) #Purple dataPin = Pin('P4', mode=Pin.IN) #Yellow

clockPin.value(0) ploadPin.value(1) pinValues = read_shift_regs() display_pin_values() oldPinValues = pinValues while True: pinValues = read_shift_regs() if(pinValues != oldPinValues): print("Pin value change detected*\r\n") print(pinValues) display_pin_values() oldPinValues = pinValues utime.sleep_ms(POLL_DELAY_MSEC)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hydronics2/2019-easy-bee-counter/issues/10, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA3MOPB7IRYLNQO5UMNGE3DRWCPN5ANCNFSM4N3HPROQ .

jhn4 commented 4 years ago

My code accualy works! The issue was the pload pin was constant 3.3V, so when i switched pins it worked fine!

You can post it on your github if you want if other people also uses microPyhton!

hydronics2 commented 4 years ago

That's great! Do you have a link to your code?

jhn4 commented 4 years ago

Here is the code, you can post it on your github! I'm not so familiar with Git and you also have the other code so it's better for other people to have all code in the same place!

This code runs on a pycom device, but i think the code will work on any micropython device since the imports are not pycom specific.

from machine import Pin import utime

How many shift register chips are daisy-chained.

NUMBER_OF_SHIFT_CHIPS = 3

Width of data (how many ext lines)

DATA_WIDTH = NUMBER_OF_SHIFT_CHIPS * 8

Width of pulse to trigger the shift register to read and latch.

PULSE_WIDTH_USEC = 5

Optional delay between shift register reads.

POLL_DELAY_MSEC = 1

pinValues = 0 oldPinValues = 0

def display_pin_values(): print("Pin states: ") for i in range(24): if(pinValues >> i & 1): print(" Pin-" , i , ": HIGH") else: print(" Pin-" , i , ": LOW") print("\r\n")

def read_shift_regs(): bitVal = 0 bytesVal = 0 ploadPin.value(0) utime.sleep_us(PULSE_WIDTH_USEC) ploadPin.value(1)

for i in range(DATA_WIDTH):

    bitVal = dataPin.value()
    # Set the corresponding bit in bytesVal.
    bytesVal |= (bitVal << ((DATA_WIDTH-1) - i))
    clockPin.value(1)
    utime.sleep_us(PULSE_WIDTH_USEC)
    clockPin.value(0)
return bytesVal

Set up

ploadPin = Pin('P23', mode=Pin.OUT) #Blue dataPin = Pin('P22', mode=Pin.IN) #Yellow clockPin = Pin('P21', mode=Pin.OUT) #PARPEL

clockPin.value(0) ploadPin.value(1)

clockPin.value(0) ploadPin.value(1)

pinValues = read_shift_regs() display_pin_values() oldPinValues = pinValues

while True: pinValues = read_shift_regs() utime.sleep_ms(POLL_DELAY_MSEC) pinValues = read_shift_regs() utime.sleep_ms(POLL_DELAY_MSEC) if(pinValues != oldPinValues): pinValues = read_shift_regs() if(pinValues != oldPinValues): print("Pin value change detected*\r\n") display_pin_values() oldPinValues = pinValues utime.sleep_ms(POLL_DELAY_MSEC)

// Johan


From: Thomas notifications@github.com Sent: Monday, July 20, 2020 7:49 PM To: hydronics2/2019-easy-bee-counter 2019-easy-bee-counter@noreply.github.com Cc: jhn4 johan.olsson4@hotmail.com; Author author@noreply.github.com Subject: Re: [hydronics2/2019-easy-bee-counter] Translate into micropython (#10)

That's great! Do you have a link to your code?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/hydronics2/2019-easy-bee-counter/issues/10#issuecomment-661238624, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AOLYYLCWU7M45B6VVB2YU6TR4R7Q5ANCNFSM4N3HPROQ.

hydronics2 commented 4 years ago

awesome.. updated the code snipit

On Tue, Jul 21, 2020 at 2:09 AM jhn4 notifications@github.com wrote:

Here is the code, you can post it on your github! I'm not so familiar with Git and you also have the other code so it's better for other people to have all code in the same place!

This code runs on a pycom device, but i think the code will work on any micropython device since the imports are not pycom specific.

from machine import Pin import utime

How many shift register chips are daisy-chained.

NUMBER_OF_SHIFT_CHIPS = 3

Width of data (how many ext lines)

DATA_WIDTH = NUMBER_OF_SHIFT_CHIPS * 8

Width of pulse to trigger the shift register to read and latch.

PULSE_WIDTH_USEC = 5

Optional delay between shift register reads.

POLL_DELAY_MSEC = 1

pinValues = 0 oldPinValues = 0

def display_pin_values(): print("Pin states: ") for i in range(24): if(pinValues >> i & 1): print(" Pin-" , i , ": HIGH") else: print(" Pin-" , i , ": LOW") print("\r\n")

def read_shift_regs(): bitVal = 0 bytesVal = 0 ploadPin.value(0) utime.sleep_us(PULSE_WIDTH_USEC) ploadPin.value(1)

for i in range(DATA_WIDTH):

bitVal = dataPin.value()

Set the corresponding bit in bytesVal.

bytesVal |= (bitVal << ((DATA_WIDTH-1) - i)) clockPin.value(1) utime.sleep_us(PULSE_WIDTH_USEC) clockPin.value(0) return bytesVal

Set up

ploadPin = Pin('P23', mode=Pin.OUT) #Blue dataPin = Pin('P22', mode=Pin.IN) #Yellow clockPin = Pin('P21', mode=Pin.OUT) #PARPEL

clockPin.value(0) ploadPin.value(1)

clockPin.value(0) ploadPin.value(1)

pinValues = read_shift_regs() display_pin_values() oldPinValues = pinValues

while True: pinValues = read_shift_regs() utime.sleep_ms(POLL_DELAY_MSEC) pinValues = read_shift_regs() utime.sleep_ms(POLL_DELAY_MSEC) if(pinValues != oldPinValues): pinValues = read_shift_regs() if(pinValues != oldPinValues): print("Pin value change detected*\r\n") display_pin_values() oldPinValues = pinValues utime.sleep_ms(POLL_DELAY_MSEC)

// Johan


From: Thomas notifications@github.com Sent: Monday, July 20, 2020 7:49 PM To: hydronics2/2019-easy-bee-counter < 2019-easy-bee-counter@noreply.github.com> Cc: jhn4 johan.olsson4@hotmail.com; Author author@noreply.github.com Subject: Re: [hydronics2/2019-easy-bee-counter] Translate into micropython (#10)

That's great! Do you have a link to your code?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub< https://github.com/hydronics2/2019-easy-bee-counter/issues/10#issuecomment-661238624>, or unsubscribe< https://github.com/notifications/unsubscribe-auth/AOLYYLCWU7M45B6VVB2YU6TR4R7Q5ANCNFSM4N3HPROQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hydronics2/2019-easy-bee-counter/issues/10#issuecomment-661735392, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA3MOPBRNEXWF5TM4KSQJUDR4VLNDANCNFSM4N3HPROQ .

hydronics2 commented 4 years ago

https://github.com/hydronics2/2019-easy-bee-counter/blob/master/arduino/python.py