From d11520bb2d1513623ce3279293507b1d35f58f4b Mon Sep 17 00:00:00 2001
From: Muhammad Hazimi Bin Yusri <mhby1g21@soton.ac.uk>
Date: Tue, 25 Feb 2025 11:21:08 +0000
Subject: [PATCH] added smooth movement

---
 wifi-test/wifi-with-ui.ino | 87 +++++++++++++++++++++++++++-----------
 1 file changed, 62 insertions(+), 25 deletions(-)

diff --git a/wifi-test/wifi-with-ui.ino b/wifi-test/wifi-with-ui.ino
index cd4dd88..2c880ab 100644
--- a/wifi-test/wifi-with-ui.ino
+++ b/wifi-test/wifi-with-ui.ino
@@ -16,10 +16,10 @@ Servo ring;
 Servo pinky;
 
 // Servo pins
-const int indexPin = 5;
-const int middlePin = 4;
-const int ringPin = 3;
-const int pinkyPin = 2;
+const int indexPin = 2;
+const int middlePin = 3;
+const int ringPin = 4;
+const int pinkyPin = 5;
 
 // Current positions
 int indexPos = 90;
@@ -27,6 +27,16 @@ int middlePos = 90;
 int ringPos = 90;
 int pinkyPos = 90;
 
+// Target positions for smooth movement
+int indexTarget = 90;
+int middleTarget = 90;
+int ringTarget = 90;
+int pinkyTarget = 90;
+
+// Movement parameters
+const int STEP_DELAY = 5;  // Delay between steps (ms)
+const int STEP_SIZE = 15;    // Size of each step in degrees
+
 void setup() {
   Serial.begin(9600);
   
@@ -62,7 +72,29 @@ void setup() {
   server.begin();
 }
 
+void moveServoSmooth(Servo &servo, int &currentPos, int targetPos) {
+  if (currentPos < targetPos) {
+    for (int pos = currentPos; pos <= targetPos; pos += STEP_SIZE) {
+      servo.write(pos);
+      currentPos = pos;
+      delay(STEP_DELAY);
+    }
+  } else {
+    for (int pos = currentPos; pos >= targetPos; pos -= STEP_SIZE) {
+      servo.write(pos);
+      currentPos = pos;
+      delay(STEP_DELAY);
+    }
+  }
+}
+
 void loop() {
+  // Handle smooth servo movements
+  if (indexPos != indexTarget) moveServoSmooth(index_f, indexPos, indexTarget);
+  if (middlePos != middleTarget) moveServoSmooth(middle, middlePos, middleTarget);
+  if (ringPos != ringTarget) moveServoSmooth(ring, ringPos, ringTarget);
+  if (pinkyPos != pinkyTarget) moveServoSmooth(pinky, pinkyPos, pinkyTarget);
+  
   WiFiClient client = server.available();
   
   if (client) {
@@ -95,6 +127,7 @@ void loop() {
           client.println(".button:hover { background: #45a049; }");
           client.println(".preset { background: #2196F3; }");
           client.println(".reset { background: #f44336; }");
+          client.println(".wave { background: #ff9800; }");
           client.println("</style>");
           client.println("</head>");
           client.println("<body>");
@@ -126,6 +159,12 @@ void loop() {
           client.println("<button class='button reset' onclick='resetPositions()'>Reset to 90°</button>");
           client.println("</div>");
           
+          // Wave motion controls
+          client.println("<div class='control-group'>");
+          client.println("<h3>Wave Motion</h3>");
+          client.println("<button class='button wave' onclick='startWave()'>Start Wave</button>");
+          client.println("</div>");
+          
           // JavaScript
           client.println("<script>");
           client.println("function updateFinger(finger, angle) {");
@@ -141,6 +180,16 @@ void loop() {
           client.println("function resetPositions() {");
           client.println("  allFingers(90);");
           client.println("}");
+          
+          client.println("function startWave() {");
+          client.println("  let fingers = ['index', 'middle', 'ring', 'pinky'];");
+          client.println("  let delay = 0;");
+          client.println("  fingers.forEach(finger => {");
+          client.println("    setTimeout(() => updateFinger(finger, 180), delay);");
+          client.println("    setTimeout(() => updateFinger(finger, 0), delay + 1000);");
+          client.println("    delay += 500;");
+          client.println("  });");
+          client.println("}");
           client.println("</script>");
           
           client.println("</body></html>");
@@ -173,34 +222,22 @@ void processRequest(String request) {
     String command = request.substring(start, end);
     Serial.println("Command: " + command);
     
-    // Parse command and move servos
+    // Parse command and set target positions
     if (command.startsWith("/index/")) {
-      int angle = command.substring(7).toInt();
-      angle = constrain(angle, 0, 180);
-      index_f.write(angle);
-      indexPos = angle;
-      Serial.println("Moving index to " + String(angle));
+      indexTarget = constrain(command.substring(7).toInt(), 0, 180);
+      Serial.println("Moving index to " + String(indexTarget));
     }
     else if (command.startsWith("/middle/")) {
-      int angle = command.substring(8).toInt();
-      angle = constrain(angle, 0, 180);
-      middle.write(angle);
-      middlePos = angle;
-      Serial.println("Moving middle to " + String(angle));
+      middleTarget = constrain(command.substring(8).toInt(), 0, 180);
+      Serial.println("Moving middle to " + String(middleTarget));
     }
     else if (command.startsWith("/ring/")) {
-      int angle = command.substring(6).toInt();
-      angle = constrain(angle, 0, 180);
-      ring.write(angle);
-      ringPos = angle;
-      Serial.println("Moving ring to " + String(angle));
+      ringTarget = constrain(command.substring(6).toInt(), 0, 180);
+      Serial.println("Moving ring to " + String(ringTarget));
     }
     else if (command.startsWith("/pinky/")) {
-      int angle = command.substring(7).toInt();
-      angle = constrain(angle, 0, 180);
-      pinky.write(angle);
-      pinkyPos = angle;
-      Serial.println("Moving pinky to " + String(angle));
+      pinkyTarget = constrain(command.substring(7).toInt(), 0, 180);
+      Serial.println("Moving pinky to " + String(pinkyTarget));
     }
   }
 }
-- 
GitLab