inteface with switches

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
atux_null
Posts: 20
Joined: Tue Jul 10, 2018 2:16 pm

inteface with switches

Post by atux_null » Mon Aug 13, 2018 12:35 pm

i am learning how to interface esp8266 nodemcu with hardware. I am following the guide from https://learn.adafruit.com/micropython- ... tal-inputs. I have attached in a linux machine the board and through

Code: Select all

screen /dev/ttyUSB 115200
i got the console of it. The i connect a cable to Ground of the board and another cable to D6. Then each end goes to the switch.
By issuing in the console

Code: Select all

>>> import machine
>>> button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
>>> button.value()
i get 1 if the button is not pressed, and if i keep the button pressed and then issue

Code: Select all

button.value()
then i get 0. That means my setup is correct as well the code. The problem is when i am doing the while loop. the code:

Code: Select all

import machine
import time
button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
    first = button.value()
    time.sleep(0.01)
    second = button.value()
    if first and not second:
        print('Button pressed!')
    elif not first and second:
        print('Button released!')
what i am doing is, while in >>> i issue ctrl-e to enter paster mode. then i paste the code. I do not exit and then i press the button on the switch. without anything to happen on the console.

another test is to stay at the >>> mode and paste the code. pressing the button nothing changes. CTRL-C exits and returns:

Code: Select all

...
Traceback (most recent call last):
  File "<stdin>", line 7
SyntaxError: invalid syntax

i am missing something over here.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: inteface with switches

Post by pythoncoder » Mon Aug 13, 2018 3:18 pm

It works on a Pyboard (just changing the pin ID) so I'm not sure what's going wrong for you.
Peter Hinch
Index to my micropython libraries.

atux_null
Posts: 20
Joined: Tue Jul 10, 2018 2:16 pm

Re: inteface with switches

Post by atux_null » Tue Aug 14, 2018 9:25 am

i was wondering if i am in a wrong state of repl and needs to be somewhere else. how can i overcome the problem and see the output on the screen please?

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

Re: inteface with switches

Post by OutoftheBOTS_ » Tue Aug 14, 2018 11:17 am

It is a bit of an unusual bit of code to do the task that your trying to do.

The switch must change state during this time.sleep(0.01) lines of code of nothing will happen

Code: Select all

    first = button.value()
    time.sleep(0.01)
    second = button.value()
You would normally do something more like this

Code: Select all

import machine
import time
button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)

#initilize old_state
old_state = button.value()

while True:    
	new_state = button.value()    
	
	#check to see if stae has changed
	if new_state != old_state:
        	if new_state: print('Button released!')
        	else: print('Button pressed!')

atux_null
Posts: 20
Joined: Tue Jul 10, 2018 2:16 pm

Re: inteface with switches

Post by atux_null » Tue Aug 14, 2018 11:38 am

thanks a lot for the reply.
the code is a copy from adafruit. as i mentioned before i am trying to learn micropython and interfacing the esp with hardware, so i used their code.
in order to run your code i am about to do the following:
-in a linux shell connect through screen to the console of the esp8266 nodemcu
-while in repl paste one line at a time your code
-await to see the outcome (button pressed...)

please acknowledge if the aforementioned steps are correct or i need something else.

I did execute your code with method i mentioned above and i cannot see anything on the console.
what am i doing wrong?

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

Re: inteface with switches

Post by OutoftheBOTS_ » Tue Aug 14, 2018 9:29 pm

Most people will usually write the code into a file with the extension on .py (e.g test_switch.py) then upload it to the board then import it (e.g import test_switch) but yes you can just paste it into the repl and this will run but you don't need to paste 1 line at a time just paste the whole script at once.

atux_null
Posts: 20
Joined: Tue Jul 10, 2018 2:16 pm

Re: inteface with switches

Post by atux_null » Wed Aug 15, 2018 6:36 am

thanks for your reply. as i mentioned above i do not see anything to happen in the console. is it something wrong with the setup?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: inteface with switches

Post by pythoncoder » Wed Aug 15, 2018 7:52 am

To paste the whole script press ctrl-e. The prompt will change as below

Code: Select all

>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== 
Then press ctrl-shift-v (if you're in a Linux console) to paste the code. Press ctrl-d to run it.

I agree with the comments of @OutoftheBOTS_ - the code isn't too clever - but it should work most of the time as the time spent in the 10ms delay will exceed that outside of it. So most switch changes will occur in that period. As I said, pasting it onto a Pyboard worked for me.
Peter Hinch
Index to my micropython libraries.

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

Re: inteface with switches

Post by OutoftheBOTS_ » Wed Aug 15, 2018 10:28 am

maybe even start with something super simple to test to see if working

Code: Select all

import machine
import time
button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)

while True:    
	print (button.value)
	time.sleep(0.2)

Post Reply