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

First time coder help
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

First time coder help

by Warlocc on Thu Jan 20, 2022 9:20 pm

Using Arduino for the first time, making my own Ghostbusters proton pack. Going for something simpler than the kits online, and trying to code it myself in the process. Just looking for some guidance- I think I'm getting this, but I'm struggling on how to cycle some of the LEDs.

So if anyone could help, my two questions are;
Am I doing this right so far?
If so, how would I cycle my circle lights, and make the muzzle flash?

Code: Select all | TOGGLE FULL SIZE
// Using neopixel LEDs, https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>

// Define all the lights (led count, pin number on board), gotta pick pins still
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, 1);
Adafruit_NeoPixel wand = Adafruit_NeoPixel(1, 1);
Adafruit_NeoPixel muzzle = Adafruit_NeoPixel(1, 1);
Adafruit_NeoPixel circle1 = Adafruit_NeoPixel(7, 1);
Adafruit_NeoPixel circle2 = Adafruit_NeoPixel(7, 1);
Adafruit_NeoPixel circle3 = Adafruit_NeoPixel(7, 1);
Adafruit_NeoPixel circle4 = Adafruit_NeoPixel(7, 1);

const int pwrtog = 18;          // pin 18 for the overall power toggle
const int trigger =20;          // pin 20 for the gun trigger
const int passpwr =13;          // pin 13 to pass through power
const int passtrg  =2;          // pin 2 for trigger passthrough

int powerstatus =0;              // Are we on or off?

void setup()
{
  pinMode(pwrtog,INPUT);         // define input
  pinMode(trigger,INPUT);         // define input
  pinMode(passpwr,OUTPUT);           // define output
  pinMode(passtrg,OUTPUT);           // define output
  strip.begin();
  strip.show();
  wand.begin();
  wand.show();
  muzzle.begin();
  muzzle.show();
  circle1.begin();
  circle1.show();
  circle2.begin();
  circle2.show();
  circle3.begin();
  circle3.show();
  circle4.begin();
  circle4.show();
}

void loop()
{
  if(digitalRead(pwrtog)==HIGH)
  {
    if(powerstatus==0)
    {
      powerstatus=1;
      digitalWrite(passpwr,HIGH);
      delay(500);
      digitalWrite(passpwr,LOW);
      strip.fill((0,0,255));
      strip.show();
      wand.fill((255,255,0));
      wand.show();
    }
    // circle1-4.fill((255,0,0)) each loop?
    if(digitalRead(trigger)==HIGH)
    {
      digitalWrite(passtrg,HIGH);
      // Neopixel muzzle light, flash orange/blue ideally? muzzle.flash?
    }
    else
    {
      digitalWrite(passtrg,LOW);
      // turn off muzzle light
    }
  }
  else
  {
    if(powerstatus==1)
    {
      powerstatus=0;
      digitalWrite(passpwr,HIGH);
      delay(500);
      digitalWrite(passpwr,LOW);
    }
    // Need to turn off all neopixels
  }
  delay(1000);
}

Warlocc
 
Posts: 7
Joined: Thu Jan 20, 2022 9:15 pm

Re: First time coder help

by adafruit_support_bill on Fri Jan 21, 2022 7:14 am

Here you are defining seven 1-pixel strips - many of them sharing the same control pin. That does not sound right at all.

Code: Select all | TOGGLE FULL SIZE
// Define all the lights (led count, pin number on board), gotta pick pins still
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, 1);
Adafruit_NeoPixel wand = Adafruit_NeoPixel(1, 1);
Adafruit_NeoPixel muzzle = Adafruit_NeoPixel(1, 1);
Adafruit_NeoPixel circle1 = Adafruit_NeoPixel(7, 1);
Adafruit_NeoPixel circle2 = Adafruit_NeoPixel(7, 1);
Adafruit_NeoPixel circle3 = Adafruit_NeoPixel(7, 1);
Adafruit_NeoPixel circle4 = Adafruit_NeoPixel(7, 1);


Please show us a photo or wiring diagram showing all of your neopixels and how they are connected.

adafruit_support_bill
 
Posts: 83839
Joined: Sat Feb 07, 2009 10:11 am

Re: First time coder help

by Warlocc on Fri Jan 21, 2022 3:20 pm

adafruit_support_bill wrote:Here you are defining seven 1-pixel strips - many of them sharing the same control pin. That does not sound right at all.

Please show us a photo or wiring diagram showing all of your neopixels and how they are connected.


Yeah, like the comment says, I still have to pick those pins and lay out my wiring, I've got a couple long ones to run. That's intentional for now. It's the other comments further in I'm more concerned about.

Warlocc
 
Posts: 7
Joined: Thu Jan 20, 2022 9:15 pm

Re: First time coder help

by adafruit_support_bill on Fri Jan 21, 2022 4:27 pm

Please show us a photo or wiring diagram showing all of your neopixels and how they are connected. We can't make any judgements on the correctness of the code if we don't know what you are attempting to control with it. Multiple 1-pixel strips is almost certainly not the best appproach.

adafruit_support_bill
 
Posts: 83839
Joined: Sat Feb 07, 2009 10:11 am

Re: First time coder help

by Warlocc on Fri Jan 21, 2022 7:28 pm

adafruit_support_bill wrote:Please show us a photo or wiring diagram showing all of your neopixels and how they are connected. We can't make any judgements on the correctness of the code if we don't know what you are attempting to control with it. Multiple 1-pixel strips is almost certainly not the best appproach.


I don't have a wiring diagram, I still have to pick the pins after I figure out how to code it on the pins. You're saying that'll work once I change the pin numbers? I still don't know how to turn off the neopixels or cycle them. Can I cycle them that way?

Warlocc
 
Posts: 7
Joined: Thu Jan 20, 2022 9:15 pm

Re: First time coder help

by adafruit_support_bill on Fri Jan 21, 2022 8:15 pm

Sorry, we can't help you without the requested information about your system. Your code is for controlling pixels and there is no way to even speculate about it's correctness unless we know what pixels are connected and how.

adafruit_support_bill
 
Posts: 83839
Joined: Sat Feb 07, 2009 10:11 am

Re: First time coder help

by Warlocc on Fri Jan 21, 2022 8:49 pm

You're telling me there's no way to know what commands control these without first wiring them up?

https://cdn.discordapp.com/attachments/ ... 21_HDR.jpg

My wiring diagram is going to be based on the kinds of answers I get. Are there commands to put all 4 circles on one pin? Do they all have to be on separate pins to cycle, with some kind of increment counter on the pins?

Warlocc
 
Posts: 7
Joined: Thu Jan 20, 2022 9:15 pm

Re: First time coder help

by adafruit_support_bill on Fri Jan 21, 2022 9:35 pm

You can chain any number of pixels on a single pin. Since we have no idea of what pixels are where or what effect you are trying to achieve, it is impossible to comment on the code.

adafruit_support_bill
 
Posts: 83839
Joined: Sat Feb 07, 2009 10:11 am

Re: First time coder help

by Warlocc on Fri Jan 21, 2022 9:45 pm

Fine, if I had to guess, it'd be something like this.
But that's the problem- now you're telling me they can be chained, which means my wiring diagram doesn't matter.

Image

Warlocc
 
Posts: 7
Joined: Thu Jan 20, 2022 9:15 pm

Re: First time coder help

by adafruit_support_bill on Fri Jan 21, 2022 10:22 pm

Based on the information provided, if pin 18 is driven high by some (undefined) external signal, the pixels connected to "strip" will turn blue and the pixels connected to "wand" will turn yellow.

Subsequent changes to the state of that pin will have no visible effect on the neopixels.

adafruit_support_bill
 
Posts: 83839
Joined: Sat Feb 07, 2009 10:11 am

Re: First time coder help

by Warlocc on Fri Jan 21, 2022 10:53 pm

adafruit_support_bill wrote:Based on the information provided, if pin 18 is driven high by some (undefined) external signal, the pixels connected to "strip" will turn blue and the pixels connected to "wand" will turn yellow.

Subsequent changes to the state of that pin will have no visible effect on the neopixels.


Oh, nice. And thank you.
Yes, I've got 5 volts of power I'll have coming in through a switch. That's the easy part. Sounds like I'm on the right track there- I just want the strip and wand to turn on solid colors while the power is on. Updating the code with those pins, I come up with this (and added more comments for what's going on);

Code: Select all | TOGGLE FULL SIZE
// Using neopixel LEDs, https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>

// Define all the lights
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, 12);
Adafruit_NeoPixel wand = Adafruit_NeoPixel(1, 11);
Adafruit_NeoPixel muzzle = Adafruit_NeoPixel(1, 10);
Adafruit_NeoPixel circle1 = Adafruit_NeoPixel(7, 9);
Adafruit_NeoPixel circle2 = Adafruit_NeoPixel(7, 8);
Adafruit_NeoPixel circle3 = Adafruit_NeoPixel(7, 7);
Adafruit_NeoPixel circle4 = Adafruit_NeoPixel(7, 6);

const int pwrtog = 18;          // pin 18 for the overall power toggle
const int trigger =20;          // pin 20 for the gun trigger
const int passpwr =13;          // pin 13 to pass through power to soundboard
const int passtrg  =2;          // pin 2 for trigger passthrough to soundboard

int powerstatus =0;              // Are we on or off?

void setup()
{
  pinMode(pwrtog,INPUT);         // define input
  pinMode(trigger,INPUT);         // define input
  pinMode(passpwr,OUTPUT);           // define output
  pinMode(passtrg,OUTPUT);           // define output
  strip.begin();
  strip.show();
  wand.begin();
  wand.show();
  muzzle.begin();
  muzzle.show();
  circle1.begin();
  circle1.show();
  circle2.begin();
  circle2.show();
  circle3.begin();
  circle3.show();
  circle4.begin();
  circle4.show();
}

void loop()
{
  if(digitalRead(pwrtog)==HIGH)
  {
    if(powerstatus==0)
    {
      // Send a brief signal on this pin when the power is first switched on.
      powerstatus=1;
      digitalWrite(passpwr,HIGH);
      delay(500);
      digitalWrite(passpwr,LOW);
      strip.fill((0,0,255)); // Turn on blue when power is on
      strip.show();
      wand.fill((255,255,0)); // Turn on yellow when power is on
      wand.show();
    }
    // circle1-4.fill((255,0,0)) cycle through them as long as power is on
    if(digitalRead(trigger)==HIGH)
    {
      digitalWrite(passtrg,HIGH);
      muzzle.rainbow(); // That doesn't seem right. Neopixel muzzle light, flash orange/blue?
    }
    else
    {
      digitalWrite(passtrg,LOW);
      muzzle.clear();
    }
  }
  else
  {
    if(powerstatus==1)
    {
      // Send a brief signal on this pin when the power is switched off.
      powerstatus=0;
      digitalWrite(passpwr,HIGH);
      delay(500);
      digitalWrite(passpwr,LOW);
    }
    // Turn all LEDs off.
    strip.clear();
    wand.clear();
    muzzle.clear();
    circle1.clear();
    circle2.clear();
    circle3.clear();
    circle4.clear();
  }
  delay(1000);
}


If I put some kind of for loop for the jewels, would a for loop go through all four all at once, or step through them incrementally each loop?

Warlocc
 
Posts: 7
Joined: Thu Jan 20, 2022 9:15 pm

Re: First time coder help

by adafruit_support_bill on Sat Jan 22, 2022 8:32 am

I've got 5 volts of power I'll have coming in through a switch.

You need a pulldown resistor also. Otherwise that input will 'float' when the switch is open.
A better approach is to connect your switch between the pin and GND. Then enable the internal pullup resistors in the UNO. https://www.arduino.cc/reference/en/lan ... o/pinmode/
This eliminates the need for the external resistor, but you will need to invert your logic to trigger when the pin state goes LOW instead of HIGH.

adafruit_support_bill
 
Posts: 83839
Joined: Sat Feb 07, 2009 10:11 am

Re: First time coder help

by Warlocc on Sun Jan 23, 2022 11:33 pm

I've got a power regulator and everything, yeah. Wiring and powering it is easy. What I can't figure out is how to cycle through the 4 jewels, or flash the muzzle LED.

Warlocc
 
Posts: 7
Joined: Thu Jan 20, 2022 9:15 pm

Re: First time coder help

by adafruit_support_bill on Mon Jan 24, 2022 7:36 am

I've got a power regulator and everything, yeah.

A regulated power supply is not the same thing as a pulldown resistor. Without a pullup or pulldown resistor on your switch input your input will float when the switch is open and the value will be unpredictable.

What I can't figure out is how to cycle through the 4 jewels, or flash the muzzle LED.

This guide shows how to program pixels on multiple pins
https://learn.adafruit.com/multi-taskin ... ino-part-3

Parts 1 and 2 will fill you in on the basic concepts.
https://learn.adafruit.com/multi-taskin ... ino-part-1
https://learn.adafruit.com/multi-taskin ... ino-part-2

adafruit_support_bill
 
Posts: 83839
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.