stumped

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

stumped

Post by uxhamby » Mon Feb 03, 2020 8:26 pm

Dear sage denizens of the Micropython forums,

Likely I am missing something simple.

I have been playing around with a bit of code to exercise an old piece of hardware that I have here. Nothing too fancy, but I am getting an error that currently has me stumped.

My code 'test3.py' currently:

Code: Select all

#!/usr/bin/env micropython

from machine import Pin, I2C
import mcp23017
import machine
import sys
import utime

i2c = I2C(scl=Pin(22), sda=Pin(21))
mcp = mcp23017.MCP23017(i2c, 0x20)

BlueLed = Pin(16,Pin.OUT) 
BlueLed.value(1)


repl_button = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)

x=0

# list interface
mcp[0].output(0)
mcp[1].output(0)
mcp[2].output(0)
mcp[3].output(0)
mcp[4].output(0)
mcp[5].output(0)
mcp[6].output(0)
mcp[7].output(0)
mcp[8].input(pull=1)
mcp[9].input(pull=1)
mcp[10].input(pull=1)
mcp[11].input(pull=1)
mcp[12].input(pull=1)
mcp[13].input(pull=1)
mcp[14].input(pull=1)
mcp[15].input(pull=1)

    
while True:

    # If button 0 is pressed, drop to REPL
    if repl_button.value() == 0:
        mcp.porta.gpio = 0
        
        #Line 44 follows next
        BlueLed.value(0)
        
        print("Dropping to REPL")
        sys.exit()

    # Turn LED on and then off
    mcp.porta.gpio = x
    #print(mcp.porta.gpio)
    utime.sleep_ms(500)
    BlueLed = not BlueLed
    x +=1
The error I get:
MicroPython v1.12-87-g96716b46e on 2020-01-26; ESP32 module with ESP32
Type "help()" for more information.
>>>
>>> import test3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test3.py", line 44, in <module>
AttributeError: 'bool' object has no attribute 'value'
Looks to me like a variable scope issue yet, I am unaware of loops posing a such a dilemma, and I can't find anything documenting variable scope specs for within while loops.

Yes, I know my code is all a bit rough around the edges, but it was never intended to be ready for prime time. This is really just an ongoing experiment and its really just this error I am seeking an assist with, thanks.

Your thoughts would be appreciated.

Cheers,

Brian H.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: stumped

Post by OutoftheBOTS_ » Mon Feb 03, 2020 9:21 pm

I think you have a namspace problem. You import machine but then use Pin

try this

Code: Select all

import machine
import sys
import utime

i2c = I2C(scl=Pin(22), sda=Pin(21))
mcp = mcp23017.MCP23017(i2c, 0x20)

BlueLed = machine.Pin(16,Pin.OUT)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: stumped

Post by jimmo » Mon Feb 03, 2020 9:38 pm

You should make the change suggested by OurOfTheBots, but the actual issue is this line

Code: Select all

BlueLed = not BlueLed
This has the effect of setting BlueLed initially to False.

You should write

Code: Select all

BlueLed.value(not BlueLed.value())

uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

Re: stumped

Post by uxhamby » Mon Feb 03, 2020 10:26 pm

Yes, this has made all the difference.

Code: Select all

BlueLed.value(not BlueLed.value())
Thanks jimmo, one and all !!

Brian H.

Post Reply