DrProg
Published © Apache-2.0

Dancing blob

Ferrofluid dances in a bottle under the influence of three electromagnets.

IntermediateFull instructions provided5 hours233
Dancing blob

Story

Read more

Custom parts and enclosures

frame

lid

Code

DANCING BLOT

Arduino
#define M1  2  // bottom magnet 1
#define M2  4  // bottom magnet 2
#define M3  3  //  top magnet

#define A   A0 // audio input

byte sound;

void setup() {
  pinMode(A, INPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
  pinMode(M3, OUTPUT);
  randomSeed(analogRead(A0));
  digitalWrite(M2, HIGH);
  digitalWrite(M1, HIGH);
  delay(200);
  digitalWrite(M3, HIGH);
  delay(500);
}


void loop() {
  load();     // reading audio
  drig();     // jerking of magnets
}


void load() {           // reading audio
  static int a[3];
  static unsigned long timer;
  if (timer > millis()) return;  // once every 50 seconds (optional)
  a[0] = a[1];
  a[1] = a[2];
  a[2] = analogRead(A);         // read and compare with previous readings, capturing the difference
  if (a[2] > a[1] + 35) {       // big splash
    sound = 2;
  } else if (a[2] > a[1] + 10) {// small splash
    sound = 1;
  } else {
    sound = 0;
  }
  timer = millis() + 50;        // polling frequency
}


void drig() {          // jerking of magnets
  static unsigned long timer;
  static byte timerOut;
  if (timer > millis()) return;        // reaction frequency (set at the end of the function)
  if (sound == 1) {                    // small splash, accidentally jerking with bottom magnets
    digitalWrite(M1, random(2));
    digitalWrite(M2, random(2));
    timerOut = 0;
  } else if (sound == 2) {             // big splash, accidentally jerking with both magnets
    int a = random(0, 30);
    if (a == 7) kl1();                 //  sometimes (with a frequency of 1/30 (optional) we go to the script (you can do a lot of them)
    digitalWrite(M1, random(2));
    digitalWrite(M2, random(2));
    digitalWrite(M3, random(10) > 6);
    timerOut = 0;
  } else {
    timerOut++;
    if (timerOut > 10) {               // turn off the magnets if there is no music for a long time
      digitalWrite(M1, LOW);
      digitalWrite(M2, LOW);
      digitalWrite(M3, LOW);
    }
  }
  timer = millis() + random(50, 200);  // random frequency from 50 to 200 ms (can be changed depending music)
}


void kl1() {			// example of script
  Serial.println("kl1");
  digitalWrite(M2, LOW);
  digitalWrite(M1, LOW);
  delay(500);
  digitalWrite(M2, HIGH);
  digitalWrite(M1, HIGH);
  delay(300);
  digitalWrite(M2, LOW);
  digitalWrite(M1, LOW);
  for (byte i = 0; i < random(3, 6); i++) {
    digitalWrite(M3, HIGH);
    delay(400);
    digitalWrite(M3, LOW);
    delay(400);
  }
}

Credits

DrProg

DrProg

1 project • 0 followers
Thanks to PCBWay.

Comments