Arduino Projects‎ > ‎

Easter Egg Random Pattern Printer



 
Bill of Materials:
  • 2 x Arduino Uno
  • 2 x Arduino Motor Shields
  • Arduino Mega
  • 2 x Hobby Servo
  • 39BYGL Stepper Motor
  • SM-42BYG011-25 Stepper Motor
  • 2 x Limit Switch
  • 2 x Resistor (10k Ohm)
Arduino Sketch: (Mega)

#include "Servo.h"
#include "Wire.h"
#include "math.h"

Servo ro;
Servo el;

int posRo = 95;
int posEl = 105;
int rollDelay = 250;
int rollCenter = 95;
int rollSpan = 0;
int rollHigh = 130;
int rollLow = 60;
int penStatus = 0;
int penDelay = 100;
int penDis = 80;
int penIng = 110;

void setup() 
{
  ro.attach(10);
  el.attach(11);
}

void loop()
  el.write(penDis);
  posEl = penDis;
  ro.write(rollCenter);
  posRo = rollCenter;
  penStatus = random(0,2);
  penDelay = random(10,350);
  rollSpan = random(20,35);
  rollDelay = random(50,350);
  rollHigh = rollCenter + rollSpan;
  rollLow = rollCenter - rollSpan;
  if (penStatus == 1)
  {
    for(posEl = penDis; posEl < penIng; posEl += 1)
    {
      el.write(posEl);
      delay(100);
    }
    for(posRo = rollCenter; posRo < rollHigh; posRo += 1)
    {
      ro.write(posRo);
      delay(rollDelay);
    }
    for(posRo = rollHigh; posRo >= rollLow; posRo -= 1)
    {
      ro.write(posRo);
      delay(rollDelay);
    }
    for(posRo = rollLow; posRo < rollCenter; posRo += 1)
    {
      ro.write(posRo);
      delay(rollDelay);
    }
    for(posEl = penIng; posEl >= penDis; posEl -= 1)
    {
      el.write(posEl);
      delay(100);
    }
  }
  if (penStatus == 0)
  {
    el.write(penDis);
    ro.write(rollCenter);
    delay(penDelay);
  }
}

Arduino Sketch: (Side Axis Uno)

const int limit1Pin = 2;
const int limit2Pin = 4;

int limit1State = 0;
int limit2State = 0;
int sideDelay = 1;
int sideSteps = 200;
int command = 0;
int Step = 0;

void setup() 
{
  pinMode(limit1Pin, INPUT);
  pinMode(limit2Pin, INPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  Serial.begin(9600); 
}

void loop(){
  sideDelay = random(1,10);
  sideSteps = random(200,2000);
  command = random(0,2);
  for(Step = 0; Step < sideSteps; Step += 1)
  {
    limit1State = digitalRead(limit1Pin);
    limit2State = digitalRead(limit2Pin);
    if (limit2State == HIGH) 
    {
      command = 0;
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, HIGH);
      analogWrite(3, 1023);
      delay(sideDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, LOW);
      analogWrite(11, 1023);
      delay(sideDelay);
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, LOW);
      analogWrite(3, 1023);
      delay(sideDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, HIGH);
      analogWrite(11, 1023);
      delay(sideDelay);
    } 
    if (limit1State == HIGH) 
    {
      command = 1;
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, HIGH);
      analogWrite(3, 255);
      delay(sideDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, HIGH);
      analogWrite(11, 255);
      delay(sideDelay);
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, LOW);
      analogWrite(3, 255);
      delay(sideDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, LOW);
      analogWrite(11, 255);
      delay(sideDelay);
    } 
    else
    {
      if (command == 0) 
      {
        digitalWrite(9, LOW);
        digitalWrite(8, HIGH);
        digitalWrite(12, HIGH);
        analogWrite(3, 1023);
        delay(sideDelay);
        digitalWrite(9, HIGH);
        digitalWrite(8, LOW);
        digitalWrite(13, LOW);
        analogWrite(11, 1023);
        delay(sideDelay);
        digitalWrite(9, LOW);
        digitalWrite(8, HIGH);
        digitalWrite(12, LOW);
        analogWrite(3, 1023);
        delay(sideDelay);
        digitalWrite(9, HIGH);
        digitalWrite(8, LOW);
        digitalWrite(13, HIGH);
        analogWrite(11, 1023);
        delay(sideDelay);
      }
      if (command == 1) 
      {
        digitalWrite(9, LOW);
        digitalWrite(8, HIGH);
        digitalWrite(12, HIGH);
        analogWrite(3, 255);
        delay(sideDelay);
        digitalWrite(9, HIGH);
        digitalWrite(8, LOW);
        digitalWrite(13, HIGH);
        analogWrite(11, 255);
        delay(sideDelay);
        digitalWrite(9, LOW);
        digitalWrite(8, HIGH);
        digitalWrite(12, LOW);
        analogWrite(3, 255);
        delay(sideDelay);
        digitalWrite(9, HIGH);
        digitalWrite(8, LOW);
        digitalWrite(13, LOW);
        analogWrite(11, 255);
        delay(sideDelay);
      }
    }
  }
}

Arduino Sketch: (Rotate Axis Uno)

int rollDelay = 100;
int command = 0;
int rollSteps = 200;
int Step = 0;

void setup() 
{
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop()
{
  command = random (0,2);
  rollDelay = random (5,50);
  rollSteps = random (10,200);
  if (command ==0)
  {
    for(Step = 0; Step < rollSteps; Step += 1)
    {
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, HIGH);
      analogWrite(3, 1023);
      delay(rollDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, LOW);
      analogWrite(11, 1023);
      delay(rollDelay);
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, LOW);
      analogWrite(3, 1023);
      delay(rollDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, HIGH);
      analogWrite(11, 1023);
      delay(rollDelay);
    }
  }
  if (command == 1)
  {
    for (Step = 0; Step < rollSteps; Step += 1)
    {
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, HIGH);
      analogWrite(3, 255);
      delay(rollDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, HIGH);
      analogWrite(11, 255);
      delay(rollDelay);
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(12, LOW);
      analogWrite(3, 255);
      delay(rollDelay);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(13, LOW);
      analogWrite(11, 255);
      delay(rollDelay);
    }
  }
}
Comments