Below is the source code for the CyberMechaBall, written mostly by Marc Kiessling:
// Cyber-Mecha-Ball Control Program // Marc Kiessling, Jason Livesey, and Mark Burger // 12/3/09 #include <Servo.h> Servo myservo; boolean onOff; boolean leftRight; //left is true, right is false. long initialTimeServo; //start a timer. this way the dc motor and servo //can opperate independently from each other. long initialTimeDC; int dcTimer; int servoTimer; void setup() { onOff = true; leftRight = true; dcTimer = random(1000,5000); servoTimer = random(1500,5000); initialTimeServo = millis(); initialTimeDC = millis(); randomSeed(analogRead(0)); pinMode(2, OUTPUT); pinMode(3, OUTPUT); myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { if ( waitDC(dcTimer)) { DCMotorToggle(); dcTimer = random(1000,5000); } if ( waitServo(servoTimer)) { turnBall(); servoTimer = random(1500,5000); } } void turnBall() { if (leftRight) { turnRight(); leftRight = false; } else { turnLeft(); leftRight = true; } } void turnRight() { myservo.write(110); delay(750); myservo.write(95); } void turnLeft() { myservo.write(80); delay(750); myservo.write(95); } void DCMotorToggle() { if (onOff) { digitalWrite(2, HIGH); digitalWrite(3, HIGH); onOff = false; } else { digitalWrite(2, LOW); digitalWrite(3, LOW); onOff = true; } } boolean waitDC(long x) { if ( millis() >= initialTimeDC + x ) { initialTimeDC = millis(); return true; } else return false; } boolean waitServo(long x) { if (millis() >= initialTimeServo + x ) { initialTimeServo = millis(); return true; } else return false; }
Leave a comment