wanniu
Published © Apache-2.0

DIY a Colorful Pyramid

This is an interactive device that looks like a pyramid, which can open and close.

BeginnerFull instructions provided3 hours78
DIY a Colorful Pyramid

Things used in this project

Story

Read more

Custom parts and enclosures

Pyramid

Code

Pyramid_code

C/C++
// demo of robot2
#include <SparkFunMiniMoto.h>  // Include the MiniMoto library
#include <Adafruit_NeoPixel.h>

#define PIN 5


// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(3, PIN, NEO_GRB + NEO_KHZ800);


// Create two MiniMoto instances, with different address settings.
MiniMoto motor0(0xC4); // A1 = 1, A0 = clear
//MiniMoto motor1(0xC0); // A1 = 1, A0 = 1 (default)

#define FAULTn  16     // Pin used for fault detection.

const int pinVoice = A0;

#define VOICE_HIGH  200

int ifMove = 0;

int voiceNormal = 0;

void voiceInit()
{
    voiceNormal = 0;
    for(int i=0; i<10; i++)
    {
        voiceNormal += getVoice();
        delay(100);
    }

    voiceNormal /= 10;

    Serial.print("voiceNormal = ");
    Serial.println(voiceNormal);
}

// 0: close
// 1: open
int status = 0;

void open()
{
    motor0.drive(-100);
    delay(2300);
    motor0.stop();
    status = 1;
}

void close()
{
    motor0.drive(100);
    delay(5000);
    motor0.stop();
    status = 0;
}

int getVoice()
{
    long sum = 0;
    for(int i=0; i<32; i++)sum+=analogRead(pinVoice);

    sum >>= 5;
    return sum;
}


bool isFirstime = 1;
void checkVoice()
{
    long sum = 0;
    for(int i=0; i<32; i++)sum+=analogRead(pinVoice);

    sum >>= 5;

    //Serial.println(sum);

    int dv = sum - voiceNormal;

    if(dv>111)
    {
        Serial.println("move...");
        
        if(isFirstime)
        {
            isFirstime = 0;return;
        }
        
        ifMove = 1;
    }
}

void testVoice()
{
    START:
    checkVoice();
    delay(10);
    goto START;
}

void setRGB(int r, int g, int b)
{
    for(int i=0; i<3; i++)
    {
        strip.setPixelColor(i, r, g, b);
    }
    strip.show();
}

void setup()
{
    Serial.begin(115200);
    Serial.println("Hello, world!");
    
    strip.begin();
    
    strip.show(); // Initialize all pixels to 'off'
    setRGB(255, 0, 0);
    pinMode(FAULTn, INPUT);

    for(int i=0; i<10; i++)checkVoice();
    close();
    
    delay(500);
    motor0.stop();
    Serial.println("sys ok");

    for(int i=0; i<100; i++)checkVoice();

    voiceInit();
    delay(1000);
    ifMove = 0;
    
    setRGB(0, 0, 0);
    //testVoice();
}

void loop()
{
    /*
    while(Serial.available())
    {
        char c = Serial.read();
        Serial.write(c);
        if(c=='1')
        {
            if(status == 0)     // close
            {
                open();
            }
            else
            {
                close();
            }
        }
    }
    */
    checkVoice();

    if(ifMove)
    {
        ifMove = 0;
        if(status == 0)     // close
        {
            move();
        }
    }
}

void move()
{
    setRGB(0, 255, 0);
    open();
    delay(1000);
    
    unsigned long timer_start = millis();
    while(1)
    {
        rainbow(50);
        if(millis()-timer_start > 5000)break;
    }
    close();
    setRGB(0, 0, 0);
    delay(1000);
    for(int i=0; i<10; i++)getVoice();
}


// 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);
    }
}

// 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) {
    if(WheelPos < 85) {
        return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
    } else if(WheelPos < 170) {
        WheelPos -= 85;
        return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
    } else {
        WheelPos -= 170;
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
    }
}

Credits

wanniu

wanniu

88 projects • 12 followers

Comments