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)
This commit is contained in:
Nicklas 2024-03-13 16:27:34 +01:00
parent 152ec40577
commit ff951f3bc5
10 changed files with 208 additions and 21 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
Assets/.DS_Store vendored

Binary file not shown.

Binary file not shown.

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 84a70b24331e346a4a3ea9eb99eaa064
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -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
}
}

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4ea5e337c5c484077bd7704cb81b5270
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -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();

@ -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
}
}

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4ea5e337c5c484077bd7704cb81b5270
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -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