Skills Dev IV: Working with Outputs - Motors and Movement

Made by Shih-Hsueh Wang

Created: November 23rd, 2022

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

#define PIXEL_PIN D2
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int motorPin = D3;



bool shouldActive = false;

void setup() {
    
    strip.begin();
    strip.show();
    
    Serial.begin(9600);
    pinMode(motorPin, OUTPUT);
    
    Particle.function ("Fan Control", FanControl);
}

void loop() {
    
    if (shouldActive){
        Serial.println ("Fan On");

        for (int i = 0; i <= 255; i++){
            analogWrite (motorPin, i);
            
            for (int k = 0; k < strip.numPixels(); k ++){
                strip.setPixelColor (k, i, i/2, i);

            }
            strip.show();
            delay(10);
        }
        
        delay (5000);
        
        for (int i = 255; i >= 0; i--){
            analogWrite (motorPin, i);
            
            for (int k = 0; k < strip.numPixels(); k ++){
                strip.setPixelColor (k, i, i/2, i);
            }
            strip.show();
            delay(10);
         
        }

      shouldActive = false;      

    }
    
}

int FanControl (String cmd){
   
    if (cmd.equals ("on")){
        shouldActive = true;
    }else if (cmd.equals ("off")){
        shouldActive = false;
    }
    
    return 1;           

}
Click to Expand
0

Outcome

A weather indicator: Use a DC motor fan with the neopixel LEDs to be a cue when the weather is not good. Video

0

Process

I attempted to build a weather indicator that uses wind force to blow up a soft fabric along with LED lights. Yet the fan was not powered enough to blow up the plastic bag so it didn't show the movement prominently.

0

Reflection

I tried to make the fan work and the LED light up at the same time. Yet, it seems there were some problems with the for statement embedded in another for statement. I assume it causes the for loop for the LED to complete first, and then the for loop for the fan starts.

0
for (int i = 0; i <= 255; i++){
            analogWrite (motorPin, i);
            
            for (int k = 0; k < strip.numPixels(); k ++){
                strip.setPixelColor (k, i, i/2, i);

            }
            strip.show();
            delay(10);
        }
Click to Expand
x