From ff951f3bc59f0a21b8f06a537aae8d70adecb7cd Mon Sep 17 00:00:00 2001 From: Nicklas <145897592+nick7ass@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:27:34 +0100 Subject: [PATCH] Touch sensor with servo finally working Added arduino scripts (TouchServo.ino is the one that is working with the water script in connection to unity) --- .DS_Store | Bin 10244 -> 10244 bytes Assets/.DS_Store | Bin 16388 -> 16388 bytes Assets/1OurESP32Scripts/.DS_Store | Bin 8196 -> 8196 bytes Assets/1OurESP32Scripts/TouchServo.meta | 8 ++ .../TouchServo/TouchServo.ino | 83 ++++++++++++++++++ .../TouchServo/TouchServo.ino.meta | 7 ++ .../WaterConnectUnityWithSensors.cs | 7 +- .../OurESP32Scripts/TouchServo/TouchServo.ino | 83 ++++++++++++++++++ .../TouchServo/TouchServo.ino.meta | 7 ++ Assets/Scenes/MainScene.unity | 34 ++++--- 10 files changed, 208 insertions(+), 21 deletions(-) create mode 100644 Assets/1OurESP32Scripts/TouchServo.meta create mode 100644 Assets/1OurESP32Scripts/TouchServo/TouchServo.ino create mode 100644 Assets/1OurESP32Scripts/TouchServo/TouchServo.ino.meta create mode 100644 Assets/OurESP32Scripts/TouchServo/TouchServo.ino create mode 100644 Assets/OurESP32Scripts/TouchServo/TouchServo.ino.meta diff --git a/.DS_Store b/.DS_Store index 031130b00109b404b0d0c1fd7d57ca5d6f1fdff8..719fe77324d89e0b481bba573c7a61123761dcef 100644 GIT binary patch delta 363 zcmZn(XbIS$B2cegueO7Mfq{iVk0G5Qlc6Lx-^C><CqD@&#u1-bC4B9|QAbqy6uj~U z8HT~h`MCu^Jq+5r1wa%t(CjjX;+%AZ3<Kj8Ua-vO#R3aim}IpkyNRYTewe&gG?OJ{ zna-ri4q_sc^~AVXjk6}i{F`hmrU+#vizzb49-A<^UCfD@@#*i$XT&5}f2f>)@oMr* zF=;4^Q(T6L$zZd(cn~Y|%58#^Yh)x??=M^lG-sxaG?cYdMuutCgvtD}vXiYO`8G4j OI&sj%6_eG)?*ahLQ+4<N delta 363 zcmZn(XbIS$B2X{6X~RJV1_l-eJ%)6KOoo!&d>5Cboctu97{`wt$2AUKIO>QhpMqDu zAj2>?IX|}msE0xFMF5Cm2AW;QP@I#FkYQlF+yIu@yjWl%3lq=6$!?-)j0+~O70qM` z^?H>$*+EQXvYr?htCz__#%q&p#S~flR|NxE$zqDk3ETpc+r^xineTW_J|iZ<x_07m z*E5q}ib+FRoZ>P}EDoF1#e-Oxk8;FKu91;ojgfo<)IC#18p_%!Bg1s8U^2g~>|`rR RzRgUsP8>9G#bkByy8wDyZ|VR5 diff --git a/Assets/.DS_Store b/Assets/.DS_Store index f88aa743c49cb5cfa5ddabd300cc154b94978957..6f1125cf742fad8fe72ba253f565cdfbf5cf85c7 100644 GIT binary patch delta 195 zcmZo^U~Fk%+;Cc$QDgEMVHwtY$;)H@O%@ap-XwB{k&$6?yqMhP*P>r|85t)(P&Q(6 zc?zUdChs@qVO{ez!u84IMiuqR1u6)hDdUI9_mx#A+p6<#eyy^Rk@4>205vI)296n7 zicw!L9CZW>JXJfx$^Zm>lh^CZOy<<**?doZ6%)|H^;%+^=WG4pVB!#+>|m?}wkpPU b@#KG#FBluL!dR-4wN3aK6*gO$oZ<!mRi{Yg delta 187 zcmZo^U~Fk%+;Cc$asK2p!ZMS2L?kweoMB{SoE$GExB0c`7hXn&$q$r`m~2#ll*;7& z#yqSich}dQo!qFRKDj^z!82t#08*(k*;bu@^J|rjjEtKn2dGJbba3$Tu*|t~;iw~6 z;HlafRz?N}2ENJb^<^e=>ho;Ar@o2_XybY<vCZ?f{%}mTGlp1nUNKhc+~f<!hO97_ Q>SS#bK1PPkRwk#o0qP$@`2YX_ diff --git a/Assets/1OurESP32Scripts/.DS_Store b/Assets/1OurESP32Scripts/.DS_Store index 4e5214c53fb5668b32a0ceb0e29c32f54fd08369..7a8b3b135b609386db7f30ef8e9cc648fe5f5382 100644 GIT binary patch delta 427 zcmZp1XmOa}&nUVvU^hRb=;Q+e9-Jw~$vH{+`8kXr-sTX2<;+MzZ-pD{85p=2LKyNH zN*R(FG8lpxQW=UE${6zD`WSF1$VoR0PR`FQ0LlXahbWN1YEW*zi%U{YeiBI0nO_mE zPc9sFL{jU4Vry=G3O>asY6~)u9iXtWFpFt3yTmt^dJMl%$*Cx|<94YcW?*7CHJu3n DBtC8X delta 41 xcmZp1XmOa}&&aniU^hP_-(*2SkIhR3mNQRI7AfAG$1{_8GrPn$mW>tc%m5(+4W$48 diff --git a/Assets/1OurESP32Scripts/TouchServo.meta b/Assets/1OurESP32Scripts/TouchServo.meta new file mode 100644 index 0000000..fdef506 --- /dev/null +++ b/Assets/1OurESP32Scripts/TouchServo.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 84a70b24331e346a4a3ea9eb99eaa064 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/1OurESP32Scripts/TouchServo/TouchServo.ino b/Assets/1OurESP32Scripts/TouchServo/TouchServo.ino new file mode 100644 index 0000000..14db969 --- /dev/null +++ b/Assets/1OurESP32Scripts/TouchServo/TouchServo.ino @@ -0,0 +1,83 @@ +#include <ArduinoWebsockets.h> +#include <WiFi.h> +#include <ESP32Servo.h> + +const char* ssid = "dsv-extrality-lab"; // Change to your WiFi network name +const char* password = "expiring-unstuck-slider"; // Change to your WiFi password + +using namespace websockets; + +WebsocketsServer server; +WebsocketsClient client; + +//#define TOUCH_PIN 10 +//#define TOUCH_THRESHOLD 100 + +bool touchDetected = false; // Flag to track touch detection +bool touchHandled = false; // Flag to track if touch has been handled + +int touchValue; + +static const int servoPin = 8; +Servo servo1; + +void setup() { + Serial.begin(115200); + + servo1.attach(servoPin); + + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi..."); + } + + Serial.print("Connected to WiFi. IP Address: "); + Serial.println(WiFi.localIP()); + + server.listen(81); + Serial.println("WebSocket server started."); +} + +void loop() { + if (server.poll()) { + client = server.accept(); + Serial.println("Client connected..."); + + while (client.available()) { + WebsocketsMessage msg = client.readBlocking(); + Serial.print("Got Message: "); + Serial.println(msg.data()); + + if (msg.data().equalsIgnoreCase("Need Touch")) { + Serial.println("Reading value from touch sensor!"); + //touchValue = touchRead(4); + Serial.println(touchRead(4)); + + while (touchRead(4) < 14000) { + if (touchRead(4) >= 14000) { + Serial.println("Value above threshold"); + + rotateServo(); + + client.send(String(touchRead(4))); + + break; + } + } + } + + } + client.close(); + } +} + + + +void rotateServo() { + // Rotate the servo motor 180 degrees + for (int posDegrees = 0; posDegrees <= 180; posDegrees++) { + servo1.write(posDegrees); + delay(10); // Adjust the delay for smooth rotation + } +} \ No newline at end of file diff --git a/Assets/1OurESP32Scripts/TouchServo/TouchServo.ino.meta b/Assets/1OurESP32Scripts/TouchServo/TouchServo.ino.meta new file mode 100644 index 0000000..2d206ab --- /dev/null +++ b/Assets/1OurESP32Scripts/TouchServo/TouchServo.ino.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4ea5e337c5c484077bd7704cb81b5270 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/1OurScripts/WaterConnectUnityWithSensors.cs b/Assets/1OurScripts/WaterConnectUnityWithSensors.cs index fc5dd28..b81f193 100644 --- a/Assets/1OurScripts/WaterConnectUnityWithSensors.cs +++ b/Assets/1OurScripts/WaterConnectUnityWithSensors.cs @@ -19,6 +19,9 @@ public class WaterConnectUnityWithSensors : MonoBehaviour public static bool isTouchDetected = false; + int threshhold = 14000; + + public BoundWaterScript waterScript = new BoundWaterScript(); void Start() @@ -64,9 +67,9 @@ public class WaterConnectUnityWithSensors : MonoBehaviour if (touchDataReceived) { - if (receivedTouchValue == 10) + if (receivedTouchValue >= threshhold) { - Debug.Log("Distance threshold exceeded, action triggered."); + Debug.Log("Touch threshold exceeded, action triggered."); isTouchDetected = true; waterScript.collectTouch(); diff --git a/Assets/OurESP32Scripts/TouchServo/TouchServo.ino b/Assets/OurESP32Scripts/TouchServo/TouchServo.ino new file mode 100644 index 0000000..14db969 --- /dev/null +++ b/Assets/OurESP32Scripts/TouchServo/TouchServo.ino @@ -0,0 +1,83 @@ +#include <ArduinoWebsockets.h> +#include <WiFi.h> +#include <ESP32Servo.h> + +const char* ssid = "dsv-extrality-lab"; // Change to your WiFi network name +const char* password = "expiring-unstuck-slider"; // Change to your WiFi password + +using namespace websockets; + +WebsocketsServer server; +WebsocketsClient client; + +//#define TOUCH_PIN 10 +//#define TOUCH_THRESHOLD 100 + +bool touchDetected = false; // Flag to track touch detection +bool touchHandled = false; // Flag to track if touch has been handled + +int touchValue; + +static const int servoPin = 8; +Servo servo1; + +void setup() { + Serial.begin(115200); + + servo1.attach(servoPin); + + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi..."); + } + + Serial.print("Connected to WiFi. IP Address: "); + Serial.println(WiFi.localIP()); + + server.listen(81); + Serial.println("WebSocket server started."); +} + +void loop() { + if (server.poll()) { + client = server.accept(); + Serial.println("Client connected..."); + + while (client.available()) { + WebsocketsMessage msg = client.readBlocking(); + Serial.print("Got Message: "); + Serial.println(msg.data()); + + if (msg.data().equalsIgnoreCase("Need Touch")) { + Serial.println("Reading value from touch sensor!"); + //touchValue = touchRead(4); + Serial.println(touchRead(4)); + + while (touchRead(4) < 14000) { + if (touchRead(4) >= 14000) { + Serial.println("Value above threshold"); + + rotateServo(); + + client.send(String(touchRead(4))); + + break; + } + } + } + + } + client.close(); + } +} + + + +void rotateServo() { + // Rotate the servo motor 180 degrees + for (int posDegrees = 0; posDegrees <= 180; posDegrees++) { + servo1.write(posDegrees); + delay(10); // Adjust the delay for smooth rotation + } +} \ No newline at end of file diff --git a/Assets/OurESP32Scripts/TouchServo/TouchServo.ino.meta b/Assets/OurESP32Scripts/TouchServo/TouchServo.ino.meta new file mode 100644 index 0000000..2d206ab --- /dev/null +++ b/Assets/OurESP32Scripts/TouchServo/TouchServo.ino.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4ea5e337c5c484077bd7704cb81b5270 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index bb95493..ed29de0 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -4672,18 +4672,6 @@ MonoBehaviour: _whenSelect: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 1293977771} - m_TargetAssemblyTypeName: Oculus.Interaction.AudioTrigger, Oculus.Interaction.OVR.Samples - m_MethodName: PlayAudio - m_Mode: 1 - m_Arguments: - m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine - m_IntArgument: 0 - m_FloatArgument: 0 - m_StringArgument: - m_BoolArgument: 0 - m_CallState: 2 - m_Target: {fileID: 1726060523} m_TargetAssemblyTypeName: BoundWaterScript, Assembly-CSharp m_MethodName: stationCompleted @@ -9512,7 +9500,7 @@ MonoBehaviour: canActivateAir: 0 narrationHasPlayed: 0 audioSource: {fileID: 171465764} - narrationClip: {fileID: 8300000, guid: 687001dfd7eb24ccca4eb0e4db510dca, type: 3} + narrationClip: {fileID: 8300000, guid: 00628ee3ba7b9f0458535254ad137558, type: 3} narrationClipTwo: {fileID: 8300000, guid: 8cb9b31b6571647878893398d64e6f19, type: 3} windObjectToCollect: {fileID: 140392492} boundControl: {fileID: 979159588} @@ -10395,7 +10383,7 @@ MonoBehaviour: narrationHasFinished: 0 narrationHasStarted: 0 audioSource: {fileID: 171465764} - narrationClip: {fileID: 8300000, guid: 1eac976b1660e489a87b946acaf94b72, type: 3} + narrationClip: {fileID: 8300000, guid: bc06e403323bf5a4ca12eef6b12390dd, type: 3} narrationClipTwo: {fileID: 8300000, guid: deff384217dfb4266aa229b913f8fd03, type: 3} fireObjectToCollect: {fileID: 282901708} boundControl: {fileID: 979159588} @@ -14125,7 +14113,7 @@ Transform: m_GameObject: {fileID: 979159584} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalPosition: {x: 0.13, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: @@ -31786,7 +31774,7 @@ MonoBehaviour: narrationHasFinished: 0 narrationHasStarted: 0 audioSource: {fileID: 171465764} - narrationClip: {fileID: 8300000, guid: 94cf8154f1b9943aba707d774a2821c7, type: 3} + narrationClip: {fileID: 8300000, guid: 1be9373128f41584db11cc06df6eb340, type: 3} narrationClipTwo: {fileID: 8300000, guid: d6a09c727429f41e1808479b9f8fd7a4, type: 3} earthObjectToCollect: {fileID: 31480962} boundControl: {fileID: 979159588} @@ -33925,7 +33913,7 @@ MonoBehaviour: dropHasAppeared: 0 narrationHasStarted: 0 audioSource: {fileID: 171465764} - narrationClip: {fileID: 8300000, guid: 08211c0f27d2740228ddd729a21f516f, type: 3} + narrationClip: {fileID: 8300000, guid: edcbc9fe06a7c054c8bc791420ddf5dc, type: 3} narrationClipTwo: {fileID: 8300000, guid: 86108e3698a924d59860ac92e3cc414a, type: 3} waterObjectToCollect: {fileID: 297646202} boundControl: {fileID: 979159588} @@ -40046,11 +40034,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 461792, guid: b5ea624fac4bcb44f96ad51d3745727e, type: 3} propertyPath: m_LocalPosition.y - value: -3.5883074 + value: -2.7 objectReference: {fileID: 0} - target: {fileID: 461792, guid: b5ea624fac4bcb44f96ad51d3745727e, type: 3} propertyPath: m_LocalPosition.z - value: -6.94 + value: -7.02 objectReference: {fileID: 0} - target: {fileID: 461792, guid: b5ea624fac4bcb44f96ad51d3745727e, type: 3} propertyPath: m_LocalRotation.w @@ -40096,6 +40084,14 @@ PrefabInstance: propertyPath: m_LocalScale.z value: 0.5 objectReference: {fileID: 0} + - target: {fileID: 470880, guid: b5ea624fac4bcb44f96ad51d3745727e, type: 3} + propertyPath: m_LocalPosition.y + value: -2.04 + objectReference: {fileID: 0} + - target: {fileID: 470880, guid: b5ea624fac4bcb44f96ad51d3745727e, type: 3} + propertyPath: m_LocalPosition.z + value: -1.58 + objectReference: {fileID: 0} - target: {fileID: 470880, guid: b5ea624fac4bcb44f96ad51d3745727e, type: 3} propertyPath: m_ConstrainProportionsScale value: 1