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
Posts: 4
Joined: Tue Jul 07, 2015 9:05 am 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 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):
  tmp = int(num / 2)
  if num % 2 == 0:
    return tmp, tmp + 10
    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

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

for i in range(10):

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

#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:
    app_running = True
  while True:
    for i in range(10):
      light = 50 + i * 10
      show_single_number(i, (light, light, light))
      if (count > cycle and random.randint(0, 9) == i) or delay > 0.01:
        delay = delay * 1.2
        if delay > 1.2:
          wait_mode = True
    count = count + 1
    if wait_mode:
      delay = 0.01
      count = 0
      wait_mode = False

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

Re: 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:
    app_running = True
but the issue didn't be fixed

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

Re: 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 Put your code in its own file - say Test it by running it at the REPL. When it's running reliably and you want it to run on boot, simply modify

Code: Select all

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

Post Reply