Color Changing Lightsaber help

So I am trying my hand at making my first lightsaber with an arduino Nano. I’m brand new to programming, but putting together bits from another persons code and creating my own, I’ve successfully gotten the lightsaber to light up and back down with the push of a button. However, now I’m trying to add in a second button to change the color, but I am at a loss. What I want to do is program the button to cycle through the colors I have so that it can light up and down with about 6 or 7 different colors. I figured to do this I would have to change the setPixelColor (color) in the light-up function. Can anyone help me write a code to do this? Thanks in advance!

(Attached is my code and a pic of my wiring)

copy_of_lightsaber1.ino (2.28 KB)

OP’s code

//I'm trying to do a lightsaber using arduino. In the final
//project I'll use an arduino nano. Here in tinkercad I can't 
//use a mp3 module and a gyroscope and accelerometer so I can't 
//to the void swing because it will be only different sounds.
//If somone have a few ideias how to make it better, send me, 
//I could use some help lol
//And I'm brazilian and sorry if a write something wrong.


//To turn on or off the blade, only press once the main button
//To turn on the animation of the crash just press once te button
//with gyroscope and accelerometer in the name, that one will
//be replace in te final project
//To turn the pierce animation on hold the secondary button
//while you press it will do the animation, let it loose to turn it of



//Library
#include <Adafruit_NeoPixel.h>

//Definitions
#define pinMatrizLED 6
#define size 20
 


//Global variables
Adafruit_NeoPixel matrizLED = Adafruit_NeoPixel(size, pinMatrizLED,NEO_GRB + NEO_KHZ800);

int i, button, bladestate; //blade state 0 off; 1 on

int buttonpushcounter = 0; //counter
int buttonstate = 0; //current state
int lastbuttonstate = 0; //previous state

int bladecolor[8][3] = { {0,0,255}, {0,255,0}, {255,0,0}, {255,128,0}, {255,255,0}, {255,0,255}, {0,255,128}, {0,255,255} };

unsigned long red = matrizLED.Color(255, 0, 0), green = matrizLED.Color(0, 255, 0), yellow = matrizLED.Color(255, 255, 0), blue = matrizLED.Color(0, 0, 255), purple = matrizLED.Color(0, 255, 255), black = matrizLED.Color(0, 0, 0);//To add a new collor use; new collor = matrizLED.Color(x, y, z)  



//Setup
void setup()
{
  matrizLED.begin();
  matrizLED.show();
  pinMode(12, INPUT);
  pinMode(3, INPUT);
  bladestate = 0;
 
}




//Useful functions
void Lightup()
{
  for(i=0; i<size; i++)
  {
    matrizLED.setPixelColor(i, blue);
    matrizLED.show();
    delay(50);
  }
}
    

void Turnoff()
{
  for(i=size-1; i>=0; i--)
  {
    matrizLED.setPixelColor(i, black);
    matrizLED.show();
    delay(50);
  }
}



//Main function
void loop(){
  button = digitalRead(12);
  if(button == HIGH){//when the main button is pressed turn on or of the blade
    switch (bladestate){
      case 0:
      	Lightup();//call the fuction
        bladestate = 1;
        break;
      case 1:
      	Turnoff();;//call the fuction
        bladestate = 0;
        break;
    }
  }
}

Please read the sticky post at the top of the forum and learn how to post your code properly. It helps people help you.

I see nothing about a second button in that code. What have you tried? People will help you, not write code for you.

pic of arduino.JPG

You need to get into the habit of connecting pushbuttons - or any switch - between an Arduino pin and ground, with a pull-up resistor to 5 V. Adjust your code to suit this. Using INPUT_PULLUP on switch inputs may suffice as the pull-up resistor without anything external to the chip, it is effectively 46k Ohms..

Your 5 V supply should connect to the "5V" pin as well as the LED strip. The LEDs draw approximately 1 mA each when dark but the Arduino also draws current, so you need to disconnect all from the battery when not in use.