Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Trying to convert my C++ to Micropython and I have questions
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trying to convert my C++ to Micropython and I have questions

by chipnod2020 on Thu Mar 11, 2021 6:06 pm

Code: Select all | TOGGLE FULL SIZE
#define NAV_LED_PIN 2

// Navigation Lights
#define NAV_LED_ON 150   
#define NAV_LED_OFF 1000
unsigned long NAV_msLast;  // last time the LED changed state
boolean NAV_ledState;    // current LED state

void setup(void)
{
  pinMode(NAV_LED_PIN, OUTPUT);
}

void loop(void)
  // Normal Stuff
  now = millis();
  // MUST call the button.loop() functions within the loop()
  buttonPC.loop();
  buttonQT.loop();
  buttonPT.loop();
  buttonWP.loop();
  buttonPH.loop();
  ledPC.loop();
  ledPH.loop();
   
  // Navigation Lights
  if (now - NAV_msLast > (NAV_ledState ? NAV_LED_ON : NAV_LED_OFF)) {
    digitalWrite(NAV_LED_PIN, NAV_ledState = !NAV_ledState);
    NAV_msLast = now;
}


Can someone give me a pointer on the proper syntax of the IF statement? The goal is to have an LED on for 500 ms, then off for 1000 ms.
This is my very broken Python code that is not complete.
Code: Select all | TOGGLE FULL SIZE
from machine import Pin
import time

led = Pin(25, Pin.OUT)

on = 500
off = 1000

led_state = 0
now = time.ticks_ms()
last = 0

while True:
    if ( now - last > ON*OFF BROKEN):
        led.toggle()
        last = time.ticks_ms()

chipnod2020
 
Posts: 27
Joined: Wed Jul 15, 2020 9:59 am

Re: Trying to convert my C++ to Micropython and I have quest

by dastels on Thu Mar 11, 2021 6:24 pm

The equivalent of
Code: Select all | TOGGLE FULL SIZE
condition ? true_value : false_value

is
Code: Select all | TOGGLE FULL SIZE
true_value if condition else false_value


So your if would look something like:
Code: Select all | TOGGLE FULL SIZE
if (now - last > (on if led_state else off)):


Dave

dastels
 
Posts: 6096
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trying to convert my C++ to Micropython and I have quest

by chipnod2020 on Mon Mar 15, 2021 2:09 pm

Thanks Dave!

So I've reworked it this far:
Code: Select all | TOGGLE FULL SIZE
import utime
from machine import Pin, PWM

#Global Variables
now = 0    #time from millis()
#Pin Definitions
NAV_LED_PIN = 4

#Navigation Lights
NAV_LED_ON = 150   
NAV_LED_OFF = 1000
NAV_msLast = 0  # last time the LED changed state
NAV_ledState = False    # current LED state

def main():
    now = utime.time_ms()
    #Navigation Lights
    if(NAV_ledState):
        if(now - NAV_msLast > NAV_LED_ON):
            NAV_LED_PIN.toggle()
            NAV_ledState = !NAV_ledState
            NAV_msLast = now
    else:
        if(now - NAV_msLast > NAV_LED_OFF):
            NAV_LED_PIN.toggle()
            NAV_ledState = !NAV_ledState
            NAV_msLast = now
main()


Thonny is complaining about NAV_ledState = !NAV_ledState

Code: Select all | TOGGLE FULL SIZE
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
  File "<stdin>", line 21
SyntaxError: invalid syntax

chipnod2020
 
Posts: 27
Joined: Wed Jul 15, 2020 9:59 am

Re: Trying to convert my C++ to Micropython and I have quest

by dastels on Mon Mar 15, 2021 4:44 pm

Use not, not !

:)

Dave

dastels
 
Posts: 6096
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trying to convert my C++ to Micropython and I have quest

by chipnod2020 on Mon Mar 15, 2021 6:20 pm

Dave, how do you tolerate people?!? ;-) Thank you so much for pointing out my Noobery!!

So I have modified the code once again to this.
Code: Select all | TOGGLE FULL SIZE
import utime
from machine import Pin, PWM

def main():
    #Pin Definition
    NAV_LED_PIN = Pin(2, Pin.OUT)
   
    #Navigation Lights
    NAV_LED_ON = 150
    NAV_LED_OFF = 1000
    NAV_msLast = 0
    NAV_ledState = False
   
    now = utime.ticks_ms()
    if(NAV_ledState):
        if(now - NAV_msLast > NAV_LED_ON):
            NAV_LED_PIN.toggle()
            NAV_ledState = not NAV_ledState
            NAV_msLast = now
    else:
        if(now - NAV_msLast > NAV_LED_OFF):
            NAV_LED_PIN.toggle()
            NAV_ledState = not NAV_ledState
            NAV_msLast = now
main()


I have a 470 ohm resistor on GPIO pin 2 and connecting it to ground of the Pico. If I do ye olde "led.on()", it works just fine. But the code does not turn up my LED. I still must be basic and missing something as I thought toggle would work.

chipnod2020
 
Posts: 27
Joined: Wed Jul 15, 2020 9:59 am

Re: Trying to convert my C++ to Micropython and I have quest

by dastels on Mon Mar 15, 2021 6:34 pm

I'm probably missing some of the finer points since it's micropython and not circuitpython.

You need a loop around that if statement (and including the assignment to now), yes?

Dave

dastels
 
Posts: 6096
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trying to convert my C++ to Micropython and I have quest

by chipnod2020 on Tue Mar 16, 2021 8:57 am

very simple solution...

I placed a while loop above the section with timers and it works great now.

Code: Select all | TOGGLE FULL SIZE
import utime
from machine import Pin, PWM

def main():
    #Pin Definition
    NAV_LED_PIN = Pin(2, Pin.OUT)
   
    #Navigation Lights
    NAV_LED_ON = 150
    NAV_LED_OFF = 1000
    NAV_msLast = 0
    NAV_ledState = False
   
    while(1):
      now = utime.ticks_ms()
      if(NAV_ledState):
         if(now - NAV_msLast > NAV_LED_ON):
            NAV_LED_PIN.toggle()
            NAV_ledState = not NAV_ledState
            NAV_msLast = now
      else:
         if(now - NAV_msLast > NAV_LED_OFF):
            NAV_LED_PIN.toggle()
            NAV_ledState = not NAV_ledState
            NAV_msLast = now
main()


Thanks for the sounding board Dave!!! I really appreciate your help!

chipnod2020
 
Posts: 27
Joined: Wed Jul 15, 2020 9:59 am

Re: Trying to convert my C++ to Micropython and I have quest

by dastels on Tue Mar 16, 2021 9:14 am

My pleasure.

Dave

dastels
 
Posts: 6096
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.