Ball Drop Game

Update

I borrowed a Nano IOT from Lejing. And it works now.

IMG_3087.MOV

Screenshot 2024-09-25 at 16.08.24.png

/*
  Joystick client using Gyroscope Data (LSM6DS3)
  Controls a networked Pong game via TCP using gyroscope data.

  This version uses WiFiNINA library for Nano 33 IoT boards.
  WiFi credentials should be set in arduino_secrets.h file.
*/

#include <SPI.h>
#include <WiFiNINA.h>  // Use this for Nano 33 IoT or MKR1010 boards
#include <Arduino_LSM6DS3.h>  // Gyroscope library for LSM6DS3

#define SECRET_SSID "sandbox370"
#define SECRET_PASS ""

// Initialize the WiFi client library
WiFiClient client;

const char server[] = "10.23.11.125";  // Server IP address
const char yourName[] = "cara";  // Client name

// Time intervals for sending messages
const int sendInterval = 50;     // Minimum time between messages to the server

long lastTimeSent = 0;  // Timestamp of the last message

// Gyroscope thresholds for movement
float x, y, z;
int plusThreshold = 30, minusThreshold = -30;

void setup() {
  Serial.begin(9600);
  while (!Serial) delay(3000);  // Wait for serial monitor to open

  // Connect to WiFi
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    WiFi.begin(SECRET_SSID, SECRET_PASS);
    delay(3000);  // Wait for connection
  }

  // Initialize the IMU (gyroscope)
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  Serial.print("Gyroscope sample rate = ");
  Serial.print(IMU.gyroscopeSampleRate());
  Serial.println(" Hz");
  
  // Print WiFi status
  printWifiStatus();

  // Connect to the server
  if (client.connect(server, 8080)) {
    Serial.println("Connected to server");
    client.print("=");
    client.println(yourName);  // Send client name
  } else {
    Serial.println("Failed to connect to server");
  }
}

void loop() {
  // Read gyroscope data
  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(x, y, z);

    // Control logic based on gyroscope readings
    if (millis() - lastTimeSent > sendInterval) {
      if (y > plusThreshold) {
        // Forward movement
        client.print("w");
        Serial.println("Forward (w)");
      } else if (y < minusThreshold) {
        // Backward movement
        client.print("s");
        Serial.println("Backward (s)");
      }

      if (x > plusThreshold) {
        // Left movement
        client.print("a");
        Serial.println("Left (a)");
      } else if (x < minusThreshold) {
        // Right movement
        client.print("d");
        Serial.println("Right (d)");
      }

      // Save this moment as the last time a message was sent
      lastTimeSent = millis();
    }
  }

  // If there's incoming data from the server, print it serially
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // If connection is lost, reconnect
  if (!client.connected()) {
    Serial.println("Disconnected. Attempting to reconnect...");
    client.stop();
    delay(1000);
    if (client.connect(server, 8080)) {
      Serial.println("Reconnected to server");
      client.print("=");
      client.println(yourName);  // Send client name again
    }
  }
}

void printWifiStatus() {
  // Print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // Print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // Print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Before that…

I'm exploring remote control options for my project. My first choice was to use the Arduino Nano IOT's built-in gyroscope sensor for the gamepad control. I like how intuitive and clean this input method is.

Unfortunately, my Nano IOT broke and the shop is out of stock. So, I started looking into other cool remote control methods. I realized iPhones have a built-in gyro sensor too. I did some digging but got a bit lost trying to figure out how to send the data to my Mac. Maybe we'll learn about using WebSocket or something similar as the class goes on? That could be a solution.

For now, I found this app called Remote Mouse as a workaround. It turns your iPhone into a remote mouse/keyboard for your Mac, using TCP/IP over Wi-Fi to communicate. It's pretty fun and works well. Not exactly what I had in mind originally, but it's a good temporary solution.

Turn iPhone, iPad and Android into wireless mobile mouse / trackpad / keyboard with Remote Mouse.