Help with Lightsaber CircuitPython code

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
vader7071
 
Posts: 124
Joined: Mon Sep 11, 2017 12:45 pm

Help with Lightsaber CircuitPython code

Post by vader7071 »

So I have the M4 feather express controller PID: 3857 and the featherwing prop shield PID: 3988. I'm following the Adafruit Propmaker Light Saber tutorial [urlhttps://learn.adafruit.com/lightsaber-featherwing/overview]Featherwing Lightsaber tutorial[/url], and I have a feature I would like to add.

To begin, I am fairly solid with Arduino coding. However, I am just learning about CircuitPython, so please forgive me if I don't immediately understand an instruction or command in Python.

Looking at the code, there is a button set to SWITCH_PIN = board.D9 and from what I can tell, this is how to adjust the way the saber acts.

What I would like to add is another button to be able to change blade colors. In Arduino, I would use 4 sets of arrays to set my RGB color codes like this:

Code: Select all

int custColRed[5] = {255,0,0,100,255}
int custColGrn[5] = {0,255,0,0,20}
int custColBlu[5] = {0,0,255,255,147}
And then, to call a custom color, use (conceptually)

Code: Select all

color 0 (red) = color(custColRed[0], custColGrn[0], custColBlu[0])
color 1 (green) = color(custColRed[1], custColGrn[1], custColBlu[1])
color 2 (blue) = color(custColRed[2], custColGrn[2], custColBlu[2])
color 3 (purple) = color(custColRed[3], custColGrn[3], custColBlu[3])
color 4 (pink) = color(custColRed[4], custColGrn[4], custColBlu[4])
And just add an input pushbutton to cycle through the color choice. Arduino wise, all easy stuff. But in CircuitPyhton, and this program, I am not so sure.

This is what I *think* I need to add:

Code: Select all

COLOR_PIN = board.D8
ccr = [255,0,0,100,255]
ccg = [0,255,0,0,20]
ccb = [0,0,255,255,147]
I know I need to identify pin D8 to be an input, but not quite sure of the syntax. Once I get through this part, the next part will be interfacing my new custom color arrays into the program to update the NeoPixels to the new colors.

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help with Lightsaber CircuitPython code

Post by dastels »

For the switch have a look at https://learn.adafruit.com/circuitpytho ... tal-in-out, specifically code related to switch in the first example.

You might also want to use the debouncer module to detect presses rather than use the value. See https://learn.adafruit.com/debouncer-li ... ns-sensors.

Instead of ccr, ccg, and so on, I would put those into another array that you index into maybe using a variable called something like current_color_index. You would start that at 0 likely, and increment it each time the color button is pressed. You'd have to handle looping around when it hits the end instead of simply incrementing:

Code: Select all

current_color_index += 1
current_color_index %= number_of_custom_colors
Dave

User avatar
vader7071
 
Posts: 124
Joined: Mon Sep 11, 2017 12:45 pm

Re: Help with Lightsaber CircuitPython code

Post by vader7071 »

Ah, thank you. I see what I was missing now. The DigitalInOut, Direction, and Pull statements. I will read up on how to implement debounce in CircuitPython.

User avatar
idy26
 
Posts: 9
Joined: Tue Dec 21, 2021 2:31 pm

Re: Help with Lightsaber CircuitPython code

Post by idy26 »

Im new to coding and this is partly what i need help with for the code. could you upload the code that has color button change please
im also looking to add a mode that does multi color but idk how

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help with Lightsaber CircuitPython code

Post by dastels »

idy26 wrote:Im new to coding and this is partly what i need help with for the code. could you upload the code that has color button change please
im also looking to add a mode that does multi color but idk how
Have you looked/search for relevant guides at https://learn.adafruit.com/?

Dave

User avatar
vader7071
 
Posts: 124
Joined: Mon Sep 11, 2017 12:45 pm

Re: Help with Lightsaber CircuitPython code

Post by vader7071 »

idy26 wrote:Im new to coding and this is partly what i need help with for the code. could you upload the code that has color button change please
im also looking to add a mode that does multi color but idk how
I'm actually working on this now.

The short version, I'm creating a few arrays that store the RGB codes for the desired colors, and using a variable to call the necessary spot out of the arrays.

I'm waiting on some JST connectors to arrive and when they get here, I'll be able to test.

User avatar
idy26
 
Posts: 9
Joined: Tue Dec 21, 2021 2:31 pm

Re: Help with Lightsaber CircuitPython code

Post by idy26 »

vader7071 wrote:
idy26 wrote:Im new to coding and this is partly what i need help with for the code. could you upload the code that has color button change please
im also looking to add a mode that does multi color but idk how
I'm actually working on this now.

The short version, I'm creating a few arrays that store the RGB codes for the desired colors, and using a variable to call the necessary spot out of the arrays.

I'm waiting on some JST connectors to arrive and when they get here, I'll be able to test.
what did you do for the momentary button code? could I see please

User avatar
vader7071
 
Posts: 124
Joined: Mon Sep 11, 2017 12:45 pm

Re: Help with Lightsaber CircuitPython code

Post by vader7071 »

idy26 wrote:what did you do for the momentary button code? could I see please
Here is what I have so far. Please note, this is snippets of the entire file and is not tested. I built this from my knowledge of arduino IDE code.

Code: Select all

from adafruit_debouncer import Debouncer

# Blade color storage arrays
ccr = [255, 0, 0, 100, 255]
ccg = [0, 255, 0, 0, 20]
ccb = [0, 0, 255, 255, 147]
cc = 0

# Input pushbutton to change blade color
COLOR_PIN = board.D8

# input button setup
bldClr = DigitalInOut(COLOR_PIN)
bldClr.direction = Direction.INPUT
bldClr.pull = Pull.UP
bladeColorPb = Debouncer(bldClr)

#in the top of the loop section to update for debounce
bldClr.update()

# on falling edge of pushbutton, increase color choice by 1
if bladeColorPb.fell:  # When colorchange PB pressed
   cc += 1
   if cc >= len(ccr):
      cc = 0  # reset index to 0
   COLOR = (ccr[cc], ccg[cc], ccb[cc])
The bottom "IF" section monitors the length of the red array, and creates a loop so the color choice rolls back to the beginning when there are no more options.

User avatar
idy26
 
Posts: 9
Joined: Tue Dec 21, 2021 2:31 pm

Re: Help with Lightsaber CircuitPython code

Post by idy26 »

Here is what I have so far. Please note, this is snippets of the entire file and is not tested. I built this from my knowledge of arduino IDE code.

Code: Select all

from adafruit_debouncer import Debouncer

# Blade color storage arrays
ccr = [255, 0, 0, 100, 255]
ccg = [0, 255, 0, 0, 20]
ccb = [0, 0, 255, 255, 147]
cc = 0

# Input pushbutton to change blade color
COLOR_PIN = board.D8

# input button setup
bldClr = DigitalInOut(COLOR_PIN)
bldClr.direction = Direction.INPUT
bldClr.pull = Pull.UP
bladeColorPb = Debouncer(bldClr)

#in the top of the loop section to update for debounce
bldClr.update()

# on falling edge of pushbutton, increase color choice by 1
if bladeColorPb.fell:  # When colorchange PB pressed
   cc += 1
   if cc >= len(ccr):
      cc = 0  # reset index to 0
   COLOR = (ccr[cc], ccg[cc], ccb[cc])
The bottom "IF" section monitors the length of the red array, and creates a loop so the color choice rolls back to the beginning when there are no more options.[/quote]

Thanks and if im understanding this right, you just have it set to 3 color options red green and blue? correct? also noticed why are you doing CMYK instead of RGB?

User avatar
vader7071
 
Posts: 124
Joined: Mon Sep 11, 2017 12:45 pm

Re: Help with Lightsaber CircuitPython code

Post by vader7071 »

The color of the blade is controlled by an RGB code. Read the "color" tag. COLOR = (255,0,0) makes red.

My section, you have to read as a place holder in each color.

So in my code, color is defined by:

COLOR = ("red array[color choice]", "green array[color choice]", "blue array[color choice]")

ccr is the red color array. Notice the 1st value in the array is 255. Then look at the 1st value in the ccg (green array) and the 1st value in the ccb (blue array). Those 2 are both 0.

So when I set cc = 0 (first position in the arrays), the following get set:

COLOR = (ccr[0], ccg[0], ccb[0])

This becomes:

COLOR = (255, 0, 0)

If I set cc (color choice) to 4, then I get:

COLOR = (ccr[4], ccg[4], ccb[4])

Which becomes:

COLOR = (255, 20, 147) and that makes a hot pink color.

Remember, when addressing an array, start counting at 0 and go up. So an array with 5 slots, you address the slots by using 0, 1, 2, 3, or 4.

User avatar
idy26
 
Posts: 9
Joined: Tue Dec 21, 2021 2:31 pm

Re: Help with Lightsaber CircuitPython code

Post by idy26 »

test the code out and it does not power up

User avatar
vader7071
 
Posts: 124
Joined: Mon Sep 11, 2017 12:45 pm

Re: Help with Lightsaber CircuitPython code

Post by vader7071 »

Do you have your "enable switch" in the closed or open position?

User avatar
idy26
 
Posts: 9
Joined: Tue Dec 21, 2021 2:31 pm

Re: Help with Lightsaber CircuitPython code

Post by idy26 »

closed, only doesn't power up with the your added code. working fine with the adafruit code untouched

User avatar
vader7071
 
Posts: 124
Joined: Mon Sep 11, 2017 12:45 pm

Re: Help with Lightsaber CircuitPython code

Post by vader7071 »

Don't mean to be a jackleg here, but I did say my code was untested, based on my arduino coding, and I am new to CircuitPython. I haven't had a chance to test my code yet, so I have no idea what it will do. I have other responsibilities right now, so it may be a few weeks before I am able to assemble all of the hardware and begin the testing phase of my new code.

Additionally, I also said the code I posted were snippets and not the entire file. What I posted will be incorporated into the example code to add features, it isn't the "entire" or complete code.

User avatar
idy26
 
Posts: 9
Joined: Tue Dec 21, 2021 2:31 pm

Re: Help with Lightsaber CircuitPython code

Post by idy26 »

I know that, also there is no D8 on the Feather M4 Express, Feather M4 Express has PWM on the following pins: A1, A3, SCK, D0, RX, D1, TX, SDA, SCL, D4, D5, D6, D9, D10, D11, D12, D13. and im all set up for testing different codes for the Saber atm. could you post code so I can play with?

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”