Project showcase

Fractal Mandelbrot With Arduino © GPL3+

Fractal Mandelbrot with Arduino with serial output.

  • 220 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

Fractal Mandelbrot with Arduino.

Code

fractal mandelbrot with arduinoC/C++
//mandelbrot06.ino
// http://rosettacode.org/wiki/Mandelbrot_set
// https://www.khanacademy.org/computer-programming/mandelbrot-set/1054272371
// https://github.com/cslarsen/mandelbrot-js
// http://falcosoft.hu/html5_mandelbrot.html
//
int pinOnBoardLED = 13;
long startTime = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pinOnBoardLED, OUTPUT);
}

void loop() {
  myMandel(-2.000, 1.000, -1.000,  1.000, 99, 50, 140);
  myMandel(-2.0262812499999998, -1.7157656249999997, 0.1242708333333333, -0.1088020833333334, 99, 50, 140);
  myMandel(0.3086785098726753, 0.5163214901273253, -0.2804711524861787, -0.4345288475138219, 150, 50, 140);
  myMandel(0.4329398558688174, 0.4498108480145078, -0.3687333735957660, -0.3812505613167620, 200, 50, 140);
  myMandel(0.4357822854352487, 0.4359042777391612, -0.3780553779480240, -0.3781461752993666, 250, 50, 140);
}

void switchLED() {
  digitalWrite(pinOnBoardLED, digitalRead(pinOnBoardLED) ^ 1);
}

void myMandel(double realMin, double realMax, double imagMin, double imagMax, int maxIterations, double ySize, double xSize) {
  // http://rosettacode.org/wiki/Mandelbrot_set#AWK
  startTime = millis();
  String myString = "\n\n\n\nrealMin=" + String(realMin) + ", realMax=" + String(realMax) + ", imagMin=" + String(imagMin) + ", imagMax=" + String(imagMax) + ", maxIterations=" + String(maxIterations) + ", startTime=" + String((startTime / 1000.0)) + "s";
  Serial.print(myString);
  //================================================================
  double xStep = (realMax - realMin) / xSize;
  double yStep = (imagMax - imagMin) / ySize;
  char* colorChar[] = {"#", "-", ":", "=", "o"};
  int colorCharLength = (sizeof(colorChar) / sizeof(colorChar[0])) - 1;
  int color = 0;
  for (int y = 0; y < ySize; y++) {
    Serial.print("\n");
    switchLED();
    double imagY = imagMin + yStep * y;
    for (int x = 0; x < xSize; x++) {
      double realX = realMin + xStep * x;
      double realZ = realX;
      double imagZ = imagY;
      for (int n = 0; n <= maxIterations; n++) {
        color = n;
        double a = realZ * realZ;
        double b = imagZ * imagZ;
        if (a + b > 4.0) break;
        imagZ = 2 * realZ * imagZ + imagY;
        realZ = a - b + realX;
      }
      (color == maxIterations ) ? color = 0 : color = color % colorCharLength + 1;
      Serial.print(colorChar[color]);
    }
  }
  //================================================================
  long elapseTime = millis() - startTime;
  myString = "\nelapseTime=" + String((elapseTime / 1000.0)) + "s";
  Serial.print(myString);
}

Comments

Similar projects you might like

A Microwave Interface for the IKEA Duktig Kids Kitchen

Project showcase by Myles Eftos

  • 1,308 views
  • 8 comments
  • 7 respects

Servo Signals and Characterization

by 3 developers

  • 1,098 views
  • 4 comments
  • 13 respects

Music Adaptive RGB LED with Arduino and Processing

Project in progress by ounane mohamed elfarouk

  • 1,016 views
  • 6 comments
  • 10 respects

Cellular IoT with Blynk & Hologram

by Moheeb Zara

  • 5,027 views
  • 9 comments
  • 18 respects

Monitoring Temperature Remotely with Blynk for Dummies

Project in progress by Cmtelesann

  • 1,224 views
  • 2 comments
  • 4 respects

How to Make a Customizable Punchable Keyboard Button

Project tutorial by Amal Mathew

  • 1,528 views
  • 4 comments
  • 4 respects
Add projectSign up / Login