How to use millis() to run periodacally code line within a milli call

you should get rid of the Serial.println(F("\n\nENTER B to get started"));and you probably don't need the timing information either, so get rid of the multipleSerial.print(F("Millis = ")); Serial.print(millis());

Your problem with theaterChase() or rainbow()is that you don't go back to the loop until it's done... You need to rewrite the functions so that they perform just 1 operation when it's time (ie their own little state machine based on timing) this way the loop checks for events for the motor, then performs 1 step of the lighting and loops. As this goes fast, you'll have the impression all is happening at the same time

for example you have this code

 void rainbow(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256; j++) {
   for(i=0; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel((i+j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

The code has a 256 steps loops (index j), which turns on all LEDs with a specific color depending on their position and the j index and display the strip, then wait for a bit. You need to rewrite that so that only 1 step is done and the call then returns.

this is what it could look like (untested, typed here):

boolean rainbowStep(uint8_t wait)
{
  static boolean rainbowStarted = false; // static means the value will get remembered across calls
  static uint16_t j;
  static unsigned long localLastChrono;

  if (rainbowStarted) {
    // we are already running, check if the wait time is over
    if (millis() - localLastChrono >= wait) { // time to act, we take 1 step
      j++;
      for (uint16_t i = 0; i < strip.numPixels(); i++)
        strip.setPixelColor(i, Wheel((i + j) & 255));
      strip.show();
      localLastChrono = millis();
      if (j >= 255) rainbowStarted = false; // we are done
    }
  } else {
    // this is the first call
    rainbowStarted = true;
    j = 0;
    for (uint16_t i = 0; i < strip.numPixels(); i++)
      strip.setPixelColor(i, Wheel((i + j) & 255));
    strip.show();
    localLastChrono = millis();
  }
  return rainbowStarted;
}

the function returns true if the rainbow is still not finished and false when it has completed a full turn.

You need to write all your functions somehow like this (again untested, but study it it should be self explanatory)

Just to chime in... if OP doesn’t actually need all the LED effects, just one particular function, then delete the unwanted clutter, and work on the output you need without the distraction.

@lastchancename: Yes, all effects are not necessary at the moment, can be worked on later. 1sly need to decide where in the code to call the pixel functions. Either inside the Ifs call of the Loop() or outside both Ifs, within Loop() method body.

see my answer.. where you put the call is in the loop. whether you write a function for it or not is irrelevant... (although I would recommend the function to make it structured and simple to read)

Here you see, I am calling it in the loop but it doesn't do anything. The pixel ring is not lighted though motor keeps working normally with sound.

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
 #include <avr/power.h>
#endif
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

//DC motor
const int pwm = 3;  //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 7 ;


//Sound sensor
const int soundSensor = 2;
boolean sensStatus = false;

enum : byte {WAITING_SOUND, SPIN_FWD, SHORT_PAUSE, SPIN_BWD} systemState;
unsigned long topChrono;
unsigned long currentDuration;
const unsigned long fwdDuration = 3000;   // 3 seconds
const unsigned long pauseDuration = 200;  // 0.2 seconds
const unsigned long bwdDuration = 3000;   // 3 seconds

boolean testSound()
{
 return (digitalRead(soundSensor) == HIGH);   //sound sensor to start motor running by a clap
}

void spinFwd()
{
   digitalWrite(in_1, LOW) ;
   digitalWrite(in_2, HIGH) ;
   analogWrite(pwm, 255) ;
}

void stopMotor()
{
  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, HIGH) ;
}

void spinBwd()
{
   digitalWrite(in_1, HIGH) ;
   digitalWrite(in_2, LOW) ;
   analogWrite(pwm, 255) ;
}

void waitForSound()
{
 systemState = WAITING_SOUND;
}

void setup() {
 Serial.begin(115200);

 pinMode(pwm,OUTPUT) ;   //we have to set PWM pin as output
 pinMode(in_1,OUTPUT) ;  //Logic pins are also set as output
 pinMode(in_2,OUTPUT) ;

 pinMode(soundSensor,INPUT) ;
 
 waitForSound();

 //Neopixel Ring
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
 if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
 // End of trinket special code
}

void loop() {

 if ((systemState == WAITING_SOUND) && testSound()) {
   systemState = SPIN_FWD;
   topChrono = millis();
   currentDuration = fwdDuration;
   spinFwd();
 }

 if ((systemState != WAITING_SOUND) && (millis() - topChrono >= currentDuration)) {
   Serial.print(F("Millis = ")); Serial.print(millis());
   switch (systemState) {
     case SPIN_FWD: // we were in fwd mode and time is up, we stop the motor and wait a bit
       systemState = SHORT_PAUSE;
       topChrono = millis();
       currentDuration = pauseDuration;
       stopMotor();
       break;

     case SHORT_PAUSE:// we were in pause mode and time is up, we go spin the other way
       systemState = SPIN_BWD;
       topChrono = millis();
       currentDuration = bwdDuration;
       spinBwd();
       break;

     case SPIN_BWD: // we were in bwd mode and time is up, we stop the motor and wait for the sound
       stopMotor();
       waitForSound();
       break;

     default:
       Serial.println(F("ERROR THIS SHOULD NOT HAPPEN!"));
       break;
   }
 }
/*  
//Neopixel Ring
// Some example procedures showing how to display to the pixels:
 colorWipe(strip.Color(255, 0, 0), 50); // Red
 colorWipe(strip.Color(0, 255, 0), 50); // Green
 colorWipe(strip.Color(0, 0, 255), 50); // Blue
 colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
 // Send a theater pixel chase in...
 theaterChase(strip.Color(127, 127, 127), 50); // White
 theaterChase(strip.Color(127, 0, 0), 50); // Red
 theaterChase(strip.Color(0, 0, 127), 50); // Blue

rainbow(20);
rainbowCycle(20);
theaterChaseRainbow(50);
*/

rainbowStep(10);

}

/*
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
 for(uint16_t i=0; i<strip.numPixels(); i++) {
   strip.setPixelColor(i, c);
   strip.show();
   delay(wait);
 }
}

void rainbow(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256; j++) {
   for(i=0; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel((i+j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
   for(i=0; i< strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
 for (int j=0; j<10; j++) {  //do 10 cycles of chasing
   for (int q=0; q < 3; q++) {
     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, c);    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
 for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
   for (int q=0; q < 3; q++) {
     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}
*/

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
 WheelPos = 255 - WheelPos;
 if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

}



boolean rainbowStep(uint8_t wait)
{
 static boolean rainbowStarted = false; // static means the value will get remembered across calls
 static uint16_t j;
 static unsigned long localLastChrono;

 if (rainbowStarted) {
   // we are already running, check if the wait time is over
   if (millis() - localLastChrono >= wait) { // time to act, we take 1 step
     j++;
     for (uint16_t i = 0; i < strip.numPixels(); i++)
       strip.setPixelColor(i, Wheel((i + j) & 255));
     strip.show();
     localLastChrono = millis();
     if (j >= 255) rainbowStarted = false; // we are done
   }
 } else {
   // this is the first call
   rainbowStarted = true;
   j = 0;
   for (uint16_t i = 0; i < strip.numPixels(); i++)
     strip.setPixelColor(i, Wheel((i + j) & 255));
   strip.show();
   localLastChrono = millis();
 }
 return rainbowStarted;
}

At a quick glance, it looks ok, but try // comment out printing millis every pass of loop()
Put a Serial.print() in when you begin each new cycle of rainbow_step()
That may help narrow down the problem

I guess it's fine. The power supply is a problem. I tried the same code on tinkercad & it turned on the neopixel. Probably the neopixel cannot draw 5 v from the uno board that is required.As same 5v from board is shared by the sounddetector and neopixel. I supplied external voltage to neopixel and it lighted up. As I have 9v at the moment at home so it behave bit strange. Tomorrow at university, I will test it with a 5V battery and see how it works and keep working on it.

Thank you very much for the help :slight_smile:

Though still it's not done. I have to put it in the lamp and see how it works. And implement the rest features later, i.e. home button so we only start it with sound and keep it working untill physically turn off and more work on neopixel. Two days in deadline so something working is needed at the moment.

Can you please tell some good simulation environment for arduino. I use tinkercad but it's not that good. For example it don't have soundsensor and sometime pins setting on it don't work on real board.

Thank you again !

What you mean by each new cycle of rainbow_step() ?

You did not rewrite any of the functions - get rid of all the old functions and all the calls just keep mine (not even sure it works) set up the a strip in setup and call just my function at the end of the loop

I kept 'uint32_t Wheel(byte WheelPos)' because you are using it in your code. Here is code, it behaves same as I described above.

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
 #include <avr/power.h>
#endif
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

//DC motor
const int pwm = 3;  //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 7 ;


//Sound sensor
const int soundSensor = 2;
boolean sensStatus = false;

enum : byte {WAITING_SOUND, SPIN_FWD, SHORT_PAUSE, SPIN_BWD} systemState;
unsigned long topChrono;
unsigned long currentDuration;
const unsigned long fwdDuration = 3000;   // 3 seconds
const unsigned long pauseDuration = 200;  // 0.2 seconds
const unsigned long bwdDuration = 3000;   // 3 seconds

boolean testSound()
{
 return (digitalRead(soundSensor) == HIGH);   //sound sensor to start motor running by a clap
}

void spinFwd()
{
   digitalWrite(in_1, LOW) ;
   digitalWrite(in_2, HIGH) ;
   analogWrite(pwm, 255) ;
}

void stopMotor()
{
  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, HIGH) ;
}

void spinBwd()
{
   digitalWrite(in_1, HIGH) ;
   digitalWrite(in_2, LOW) ;
   analogWrite(pwm, 255) ;
}

void waitForSound()
{
 systemState = WAITING_SOUND;
}

void setup() {
 Serial.begin(115200);

 pinMode(pwm,OUTPUT) ;   //we have to set PWM pin as output
 pinMode(in_1,OUTPUT) ;  //Logic pins are also set as output
 pinMode(in_2,OUTPUT) ;

 pinMode(soundSensor,INPUT) ;
 
 waitForSound();

}

void loop() {
 if ((systemState == WAITING_SOUND) && testSound()) {
   systemState = SPIN_FWD;
   topChrono = millis();
   currentDuration = fwdDuration;
   spinFwd();
 }

 if ((systemState != WAITING_SOUND) && (millis() - topChrono >= currentDuration)) {
    switch (systemState) {
     case SPIN_FWD: // we were in fwd mode and time is up, we stop the motor and wait a bit
       systemState = SHORT_PAUSE;
       topChrono = millis();
       currentDuration = pauseDuration;
       stopMotor();
       break;

     case SHORT_PAUSE:// we were in pause mode and time is up, we go spin the other way
       systemState = SPIN_BWD;
       topChrono = millis();
       currentDuration = bwdDuration;
       spinBwd();
       break;

     case SPIN_BWD: // we were in bwd mode and time is up, we stop the motor and wait for the sound
       stopMotor();
       waitForSound();
       break;

     default:
       Serial.println(F("ERROR THIS SHOULD NOT HAPPEN!"));
       break;
   }
 }
rainbowStep(50);
}


uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
  return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
  WheelPos -= 85;
  return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

boolean rainbowStep(uint8_t wait)
{
 static boolean rainbowStarted = false; // static means the value will get remembered across calls
 static uint16_t j;
 static unsigned long localLastChrono;

 if (rainbowStarted) {
   // we are already running, check if the wait time is over
   if (millis() - localLastChrono >= wait) { // time to act, we take 1 step
     j++;
     for (uint16_t i = 0; i < strip.numPixels(); i++)
       strip.setPixelColor(i, Wheel((i + j) & 255));
     strip.show();
     localLastChrono = millis();
     if (j >= 255) rainbowStarted = false; // we are done
   }
 } else {
   // this is the first call
   rainbowStarted = true;
   j = 0;
   for (uint16_t i = 0; i < strip.numPixels(); i++)
     strip.setPixelColor(i, Wheel((i + j) & 255));
   strip.show();
   localLastChrono = millis();
 }
 return rainbowStarted;
}

Sorry I had not seen on my iPhone that you had commented out the lines with /* and */

So when you say "The pixel ring is not lighted" - have you ever seen them working? can you provide a link to your Strip?

try wiring just the strip on pin 6, remove everything else, ensure the strip is correctly powered with a high power source, separately from the arduino, and that grounds are joined

then try this code to see if the small piece of code I wrote does work

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
const byte stripPin = 6;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, stripPin, NEO_GRB + NEO_KHZ800);

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

boolean rainbowStep(uint8_t wait)
{
  static boolean rainbowStarted = false; // static means the value will get remembered across calls
  static uint16_t j;
  static unsigned long localLastChrono;

  if (rainbowStarted) {
    // we are already running, check if the wait time is over
    if (millis() - localLastChrono >= wait) { // time to act, we take 1 step
      j++;
      for (uint16_t i = 0; i < strip.numPixels(); i++)
        strip.setPixelColor(i, Wheel((i + j) & 255));
      strip.show();
      localLastChrono = millis();
      if (j >= 255) rainbowStarted = false; // we are done
    }
  } else {
    // this is the first call
    rainbowStarted = true;
    j = 0;
    for (uint16_t i = 0; i < strip.numPixels(); i++)
      strip.setPixelColor(i, Wheel((i + j) & 255));
    strip.show();
    localLastChrono = millis();
  }
  return rainbowStarted;
}


void setup() {
  Serial.begin(115200);
}

void loop() {
  rainbowStep(100);
}

just to test strip, I uploaded the adafruit code & it lighted the strip. You current piece didn't light it up.

can you show me a small piece of adafruit code that works? just to ensure we don't miss any initialization or whatever...

Second thought: if I remember correctly may be we need in the setup()

   strip.begin();
   strip.show(); // strip to black

try this

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
const byte stripPin = 6;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, stripPin, NEO_GRB + NEO_KHZ800);

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

boolean rainbowStep(uint8_t wait)
{
  static boolean rainbowStarted = false; // static means the value will get remembered across calls
  static uint16_t j;
  static unsigned long localLastChrono;

  if (rainbowStarted) {
    // we are already running, check if the wait time is over
    if (millis() - localLastChrono >= wait) { // time to act, we take 1 step
      j++;
      for (uint16_t i = 0; i < strip.numPixels(); i++)
        strip.setPixelColor(i, Wheel((i + j) & 255));
      strip.show();
      localLastChrono = millis();
      if (j >= 255) rainbowStarted = false; // we are done
    }
  } else {
    // this is the first call
    rainbowStarted = true;
    j = 0;
    for (uint16_t i = 0; i < strip.numPixels(); i++)
      strip.setPixelColor(i, Wheel((i + j) & 255));
    strip.show();
    localLastChrono = millis();
  }
  return rainbowStarted;
}


void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.show()
}

void loop() {
  rainbowStep(100);
}

This is neopixel link:

adafruit code is this one.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h>
#endif

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {
 // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
 #if defined (__AVR_ATtiny85__)
   if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
 #endif
 // End of trinket special code


 strip.begin();
 strip.show(); // Initialize all pixels to 'off'
}

void loop() {
 // Some example procedures showing how to display to the pixels:
 colorWipe(strip.Color(255, 0, 0), 50); // Red
 colorWipe(strip.Color(0, 255, 0), 50); // Green
 colorWipe(strip.Color(0, 0, 255), 50); // Blue
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
 // Send a theater pixel chase in...
 theaterChase(strip.Color(127, 127, 127), 50); // White
 theaterChase(strip.Color(127, 0, 0), 50); // Red
 theaterChase(strip.Color(0, 0, 127), 50); // Blue

 rainbow(20);
 rainbowCycle(20);
 theaterChaseRainbow(50);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
 for(uint16_t i=0; i<strip.numPixels(); i++) {
   strip.setPixelColor(i, c);
   strip.show();
   delay(wait);
 }
}

void rainbow(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256; j++) {
   for(i=0; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel((i+j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
   for(i=0; i< strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
 for (int j=0; j<10; j++) {  //do 10 cycles of chasing
   for (int q=0; q < 3; q++) {
     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, c);    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
 for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
   for (int q=0; q < 3; q++) {
     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
 WheelPos = 255 - WheelPos;
 if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

no, this updated code doesn't work.

what about this one? do you see the strip going red, green, blue?

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
const byte stripPin = 6;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, stripPin, NEO_GRB + NEO_KHZ800);

void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.show();
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue
}

void loop() {}

Yes, this work. But infect the last code also work where strip was initialized in set up. Appears there was something loose with connections, I wonder though same connection always work with adafruit code.

OK... make sure ground are shared and tight

This is my full code. One thing is neopixel is lighted when the circuit is one, it doesn't start with the motor after receiving sound signal. That is not a problem or required behavior but it looks nice if everything starts working with sound signal

CODE:
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
const byte stripPin = 6;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, stripPin, NEO_GRB + NEO_KHZ800);


//DC motor
const int pwm = 3;  //initializing pin 3 as pwm
const int in_1 = 8 ;
const int in_2 = 7 ;


//Sound sensor
const int soundSensor = 2;
boolean sensStatus = false;

enum : byte {WAITING_SOUND, SPIN_FWD, SHORT_PAUSE, SPIN_BWD} systemState;
unsigned long topChrono;
unsigned long currentDuration;
const unsigned long fwdDuration = 3000;   // 3 seconds
const unsigned long pauseDuration = 200;  // 0.2 seconds
const unsigned long bwdDuration = 3000;   // 3 seconds

boolean testSound()
{
 return (digitalRead(soundSensor) == HIGH);   //sound sensor to start motor running by a clap
}

void spinFwd()
{
   digitalWrite(in_1, LOW) ;
   digitalWrite(in_2, HIGH) ;
   analogWrite(pwm, 255) ;
}

void stopMotor()
{
  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, HIGH) ;
}

void spinBwd()
{
   digitalWrite(in_1, HIGH) ;
   digitalWrite(in_2, LOW) ;
   analogWrite(pwm, 255) ;
}

void waitForSound()
{
 systemState = WAITING_SOUND;
}

void setup() {
 Serial.begin(115200);

 pinMode(pwm,OUTPUT) ;   //we have to set PWM pin as output
 pinMode(in_1,OUTPUT) ;  //Logic pins are also set as output
 pinMode(in_2,OUTPUT) ;
 pinMode(soundSensor,INPUT) ;
   
 strip.begin();
 strip.show();
   
}

void loop() {
 if ((systemState == WAITING_SOUND) && testSound()) {
   systemState = SPIN_FWD;
   topChrono = millis();
   currentDuration = fwdDuration;
   spinFwd();
 }

 if ((systemState != WAITING_SOUND) && (millis() - topChrono >= currentDuration)) {
    switch (systemState) {
     case SPIN_FWD: // we were in fwd mode and time is up, we stop the motor and wait a bit
       systemState = SHORT_PAUSE;
       topChrono = millis();
       currentDuration = pauseDuration;
       stopMotor();
       break;

     case SHORT_PAUSE:// we were in pause mode and time is up, we go spin the other way
       systemState = SPIN_BWD;
       topChrono = millis();
       currentDuration = bwdDuration;
       spinBwd();
       break;

     case SPIN_BWD: // we were in bwd mode and time is up, we stop the motor and wait for the sound
       stopMotor();
       waitForSound();
       break;

     default:
       Serial.println(F("ERROR THIS SHOULD NOT HAPPEN!"));
       break;
   }
 }
rainbowStep(100);
}

uint32_t Wheel(byte WheelPos) {
 WheelPos = 255 - WheelPos;
 if (WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if (WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

boolean rainbowStep(uint8_t wait)
{
 static boolean rainbowStarted = false; // static means the value will get remembered across calls
 static uint16_t j;
 static unsigned long localLastChrono;

 if (rainbowStarted) {
   // we are already running, check if the wait time is over
   if (millis() - localLastChrono >= wait) { // time to act, we take 1 step
     j++;
     for (uint16_t i = 0; i < strip.numPixels(); i++)
       strip.setPixelColor(i, Wheel((i + j) & 255));
     strip.show();
     localLastChrono = millis();
     if (j >= 255) rainbowStarted = false; // we are done
   }
 } else {
   // this is the first call
   rainbowStarted = true;
   j = 0;
   for (uint16_t i = 0; i < strip.numPixels(); i++)
   strip.setPixelColor(i, Wheel((i + j) & 255));
   strip.show();
   localLastChrono = millis();
 }
 return rainbowStarted;
}

No need to put wrong comments in the code... this is confusing everyone

const int pwm = 3;  //initializing pin 2 as pwm