main.py not running as expected

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jszsj
Posts: 4
Joined: Tue Jul 07, 2015 9:05 am

main.py not running as expected

Post by jszsj » Fri Nov 03, 2017 10:42 am

hello, my English is not very well, but I'm working on it
I'm using neopixel to control leds, when I run the code directly in the console,everything works fine, but when I upload main.py to the board,after the first boot it's not running as expected, then press the reboot button it just works, I don't know how to find out what's wrong with my code:

Code: Select all

import machine
import neopixel
import time
import random
# from ntptime import settime

brightSteps = 20
brightness = 3

pin = machine.Pin(14, machine.Pin.OUT) # D5
np = neopixel.NeoPixel(pin, 20, timing=True)

# 单个数字的初始颜色序列
blank_colors = [(0, 0, 0) for _ in range(20)]


def get_number_index(num):
  """获取点亮数字num的led下标"""
  tmp = int(num / 2)
  if num % 2 == 0:
    return tmp, tmp + 10
  else:
    return 9 - tmp, 19 - tmp


def get_color_number(num, color):
  """获取指定颜色的数字序列"""
  sequence = blank_colors[:]
  led1, led2 = get_number_index(num)
  sequence[led1] = color
  sequence[led2] = color
  return sequence

def show_single_number(num, color):
  colors = get_color_number(num, color)
  for i, c in enumerate(colors):
    np[i] = c
  np.write()


def clear():
  for i, c in enumerate(blank_colors):
    np[i] = c
  np.write()
  


for i in range(10):
  print(get_number_index(i))


def do_connect(ssid, passwd):
  import network
  wlan = network.WLAN(network.STA_IF)
  wlan.active(True)
  if not wlan.isconnected():
      print('connecting to network...')
      wlan.connect(ssid, passwd)
      while not wlan.isconnected():
          pass
  print('network config:', wlan.ifconfig())
  start_loop()


#do_connect('_python_', 'py87654331')
delay = 0.01
cycle = 3
wait_mode = False
count = 0

app_running = False

def main():
  global delay
  global cycle
  global wait_mode
  global count
  global app_running
  
  if app_running:
    return
  else:
    app_running = True
  
  while True:
    for i in range(10):
      light = 50 + i * 10
      show_single_number(i, (light, light, light))
      time.sleep(delay)
      if (count > cycle and random.randint(0, 9) == i) or delay > 0.01:
        delay = delay * 1.2
        if delay > 1.2:
          wait_mode = True
          break
      clear()
    count = count + 1
    if wait_mode:
      print(i)
      time.sleep(5)
      delay = 0.01
      count = 0
      wait_mode = False
      
main()

jszsj
Posts: 4
Joined: Tue Jul 07, 2015 9:05 am

Re: main.py not running as expected

Post by jszsj » Fri Nov 03, 2017 10:48 am

It's feel like the code start to run but after a while it start over then crash, so I add these code to main():

Code: Select all

  if app_running:
    return
  else:
    app_running = True
but the issue didn't be fixed

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

Re: main.py not running as expected

Post by pythoncoder » Sat Nov 04, 2017 7:38 am

In general it's easier to debug programs if you don't put them in main.py. Put your code in its own file - say mymodule.py. Test it by running it at the REPL. When it's running reliably and you want it to run on boot, simply modify main.py:

Code: Select all

import mymodule
mymodule.run()  # Unless you've written it to run on import
Peter Hinch

Post Reply