Updated Peeper Code 1
#include <Servo.h>
// -----------------------
// Servo objects
// -----------------------
Servo servoLF;
Servo servoRF;
Servo servoLB;
Servo servoRB;
// -----------------------
// Pins
// -----------------------
const int pinLF = 11;
const int pinRF = 10;
const int pinLB = 9;
const int pinRB = 8;
const int LED_PIN = 4;
// -----------------------
// Calibration directions
// -----------------------
int dirLF = +1;
int dirRF = -1;
int dirLB = -1;
int dirRB = +1;
// -----------------------
// Motion parameters
// -----------------------
const int neutralPos = 90;
const int moveOffset = 30;
// Smooth movement parameters (50% faster)
const int smoothSteps = 25;
const int smoothDelay = 5; // was 10 → now 50% faster
// -----------------------
// LED blink timing
// -----------------------
unsigned long lastBlink = 0;
unsigned long blinkInterval = 0;
bool ledBlinking = false;
unsigned long blinkPhaseStart = 0;
// -----------------------
// RUNTIME SETTINGS
// -----------------------
const unsigned long TOTAL_RUNTIME = 600000UL; // 10 min
unsigned long startTime;
// walking for 10s – 90s
unsigned long minWalk = 10000;
unsigned long maxWalk = 90000;
// break for 10s – 30s
unsigned long minBreak = 10000;
unsigned long maxBreak = 30000;
// -----------------------
// Smooth movement function
// -----------------------
void moveSmooth(Servo &s, int start, int end) {
float inc = float(end - start) / smoothSteps;
for (int i = 0; i <= smoothSteps; i++) {
s.write(start + inc * i);
delay(smoothDelay);
}
}
// -----------------------
// LED behavior
// -----------------------
void updateLed() {
unsigned long now = millis();
if (!ledBlinking) {
if (now - lastBlink >= blinkInterval) {
ledBlinking = true;
blinkPhaseStart = now;
digitalWrite(LED_PIN, LOW);
}
}
else {
if (now - blinkPhaseStart >= 500 && now - blinkPhaseStart < 1000) {
digitalWrite(LED_PIN, HIGH);
}
else if (now - blinkPhaseStart >= 1000) {
ledBlinking = false;
lastBlink = now;
blinkInterval = random(5000, 10000);
digitalWrite(LED_PIN, HIGH);
}
}
}
// -----------------------
// One walking step cycle
// -----------------------
void performStep(bool forwardPhase) {
int LFtarget = neutralPos + dirLF * (forwardPhase ? moveOffset : -moveOffset);
int RFtarget = neutralPos + dirRF * (forwardPhase ? -moveOffset : moveOffset);
int LBtarget = neutralPos + dirLB * (forwardPhase ? -moveOffset : moveOffset);
int RBtarget = neutralPos + dirRB * (forwardPhase ? moveOffset : -moveOffset);
moveSmooth(servoLF, servoLF.read(), LFtarget);
moveSmooth(servoRF, servoRF.read(), RFtarget);
moveSmooth(servoLB, servoLB.read(), LBtarget);
moveSmooth(servoRB, servoRB.read(), RBtarget);
}
// -----------------------
// NEUTRAL pose
// -----------------------
void goNeutral() {
moveSmooth(servoLF, servoLF.read(), neutralPos);
moveSmooth(servoRF, servoRF.read(), neutralPos);
moveSmooth(servoLB, servoLB.read(), neutralPos);
moveSmooth(servoRB, servoRB.read(), neutralPos);
}
// -----------------------
// Setup
// -----------------------
void setup() {
servoLF.attach(pinLF);
servoRF.attach(pinRF);
servoLB.attach(pinLB);
servoRB.attach(pinRB);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
blinkInterval = random(5000, 10000);
goNeutral();
delay(500);
startTime = millis(); // Start 10-minute timer
}
// -----------------------
// Main Loop
// -----------------------
void loop() {
updateLed();
unsigned long now = millis();
if (now - startTime >= TOTAL_RUNTIME) {
goNeutral();
return; // stop completely after 10 minutes
}
// -------- WALK PHASE --------
unsigned long walkTime = random(minWalk, maxWalk);
unsigned long walkEnd = millis() + walkTime;
while (millis() < walkEnd && millis() - startTime < TOTAL_RUNTIME) {
updateLed();
performStep(true);
performStep(false);
}
// -------- BREAK PHASE --------
goNeutral();
unsigned long breakTime = random(minBreak, maxBreak);
unsigned long breakEnd = millis() + breakTime;
while (millis() < breakEnd && millis() - startTime < TOTAL_RUNTIME) {
updateLed();
// remain still
}
}















