Emmett Walter
Published

Northern Lights Visualization Scene

This scene represents the present visibility of the northern lights over the mountain Kirkjufell in Iceland.

AdvancedShowcase (no instructions)10 hours17
Northern Lights Visualization Scene

Things used in this project

Hardware components

Photon 2
Particle Photon 2
×1
Stepper Motor
Digilent Stepper Motor
×1
SparkFun Snappable Protoboard
SparkFun Snappable Protoboard
Green version
×1
5V 2.5A Switching Power Supply
Digilent 5V 2.5A Switching Power Supply
×1
5v DC Adapter Female
×1
Adafruit Neopixel (Silicone coated)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solid Core Wire
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Code

Northern Lights

C/C++
#include <neopixel.h>
#include <AccelStepper.h>
#include <ArduinoJson.h>
#include "Particle.h"

JsonDocument doc;

SYSTEM_MODE(AUTOMATIC);

SerialLogHandler logHandler(LOG_LEVEL_INFO);

#define HALFSTEP 8       //--> 2048 step. Si Halfstep 8 --> 4096 pas
#define motor1Pin1 D0
#define motor1Pin2 D1
#define motor1Pin3 D2
#define motor1Pin4 D4


AccelStepper stepper1(HALFSTEP, motor1Pin1, motor1Pin3, motor1Pin2, motor1Pin4);

int endPoint1 = 8192; 


int LED_Count = 41;

Adafruit_NeoPixel light(LED_Count, SPI, WS2812B);

int lightDelay = 50;
float brightnessOfLed = 1;
int speed = 300;
int minSpeed = 200;//200
int maxSpeed = 500;
float maxLightVal = 20.0;


int previous1 = -1;
int previous2 = -1;
int previous3 = -1;
int previous4 = -1;


void getData(){
    Particle.publish("getNLData");
}
int w = 0;
int v = 0;
int k = 0;
int n = 0;
void colorAnimation(){
    //add usage of lightspeed
    if(w<LED_Count){
        light.setPixelColor(w, 0 * brightnessOfLed, 184 * brightnessOfLed, 22 * brightnessOfLed);
        light.show();
        w++;
    }
   
    
    if(w >= LED_Count && v < LED_Count){
        light.setPixelColor(v, 0 * brightnessOfLed, 145 * brightnessOfLed, 19 * brightnessOfLed);
        light.show();
        v++;
    }
    
    
    if(v>=LED_Count){
        w=0;
        v=0;
    }
}

Timer getTimer(30000, getData); // Run getData every 1000ms outside of the void loop
Timer lightMovement(lightDelay, colorAnimation);


void setup() {
    while(Particle.disconnected());
    Serial.println("Particle connected to cloud!");
    Particle.subscribe("hook-response/getNLData", extractInfo);
    Serial.begin(9600);
    stepper1.setMaxSpeed(1000);
    stepper1.setAcceleration(100000);
    stepper1.setSpeed(speed);
    light.begin();
    light.show();
    light.setBrightness(50);
    light.show();
    getTimer.start();
    lightMovement.start();
}

void loop() {

    
    if (stepper1.distanceToGo() == 0) {
        stepper1.setCurrentPosition(0);
        endPoint1 = -endPoint1;
        stepper1.moveTo(endPoint1);
    }
    
    stepper1.run();

  
    // Run both motors simultaneously
    
    
    
}

void extractInfo(const char *event, const char *data)
{
    Serial.println("Handler is starting...");
    deserializeJson(doc, data);
    Serial.println("Data Deserialized and is available for analysis.");

    JsonArray arr = doc["selected"];    
    
    const char* p1 = arr[0];
    const char* p2 = arr[1];
    const char* p3 = arr[2];
    const char* p4 = arr[3];
    
    std::string st1(p1);
    std::string str1 = st1.substr(7); 
    int sub1 = std::stoi(str1);  
    
    std::string st2(p2);
    std::string str2 = st2.substr(7); 
    int sub2 = std::stoi(str2);  
    
    std::string st3(p3);
    std::string str3 = st3.substr(7); 
    int sub3 = std::stoi(str3);  
    
    std::string st4(p4);
    std::string str4 = st4.substr(7); 
    int sub4 = std::stoi(str4);  


    if(previous1 == -1){
        previous1 = sub1;
        previous2 = sub2;
        previous3 = sub3;
        previous4 = sub4;
        changeSpeed(sub1, sub2, sub3, sub4);
    } else if(previous1 != sub1 || previous2 != sub2 || previous3 != sub3 || previous4 != sub4){
        changeSpeed(sub1, sub2, sub3, sub4);
        previous1 = sub1;
        previous2 = sub2;
        previous3 = sub3;
        previous4 = sub4;
    }
    
    
    Serial.println(sub1);
    Serial.println(sub2);
    Serial.println(sub3);
    Serial.println(sub4);
}

void changeSpeed(int a, int b, int c, int d){
    float avgnum = (a+b+c+d)/4.0;
    float percentageSpeed = avgnum/100.0;
    float percentForUse = avgnum/maxLightVal;
    if (percentForUse>1.0){
        percentForUse = 1.0;
    }
    speed = (percentageSpeed * (maxSpeed - minSpeed)) + minSpeed;
    Serial.println(speed);
    Serial.println(percentForUse);
    stepper1.setSpeed(maxSpeed * percentForUse); //just use variable speed for more accurate and less drastic change.
    brightnessOfLed = percentForUse; //use percentageSpeed variable for more accurate and less drastic change.
}

Credits

Emmett Walter
2 projects • 0 followers
Thanks to Elio.

Comments