Help Improve Interactive Game setup

Hello, I need some help/advice on how to improve my interactive game setup. I am trying to create an interactive agility light pod system where when one lights up you run to that pod and it will register with a hand and then randomly choose another pod to light up.

Any help is greatly appreciated!

Here is a list of everything I am using.

  1. NodeMCU ESP8266 CP2102 ESP-12E Module:
  2. NeoPixel LED Strip: 30 Leds
  3. TCRT5000 IR Sensor
  4. Power Supply using a 18650 battery with a capacity of 3300mAh and a TP4056 charger module with an integrated step-up converter.
  5. Resistors and Capacitors: For protection I pur a 1000µF capacitor across the power supply lines.

The setup:

Here is the code.

#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <espnow.h>

#define RGBDATAPIN D2 // Adjust if needed, GPIO4
#define RGBLEDNUM 30
#define IRSENSOR_PIN D1 // Digital pin connected to the IR sensor, GPIO5
#define MY_ECU 3 // Change for each module (1, 2, 3)
#define ACTIVITY_TIMEOUT 120000 // 2 minutes of inactivity

// Colors defined as RGB tuples
#define COLOR_HAND_DETECTED  pixels.Color(50, 50, 0) // Yellow
#define COLOR_COMM_SUCCESS   pixels.Color(0, 255, 0) // Green

Adafruit_NeoPixel pixels(RGBLEDNUM, RGBDATAPIN, NEO_GRB + NEO_KHZ800);

// Replace these with the MAC addresses of your other modules
uint8_t receiverAddresses[][6] = {
    {0x48, 0x55, 0x19, 0xED, 0xC9, 0x98}, // Module 1 MAC
    {0x48, 0x55, 0x19, 0xED, 0xAA, 0xA4}, // Module 2 MAC
    {0x48, 0x55, 0x19, 0xED, 0x0A, 0xD8}  // Module 3 MAC
};

struct dataPacket {
  int LED_Token = MY_ECU;
};
dataPacket packet;

unsigned long lastActivityTime = millis();

void setup() {
  Serial.begin(115200);
  pixels.begin();
  pixels.show(); // Turn off all LEDs at the start

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  if (esp_now_init() != 0) {
    Serial.println("ESP-NOW initialization failed");
    return;
  }

  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);

  // Add ESP-NOW peers
  for (int i = 0; i < sizeof(receiverAddresses) / 6; i++) {
    if (i != MY_ECU - 1) { // Exclude its own address
      esp_now_add_peer(receiverAddresses[i], ESP_NOW_ROLE_COMBO, 1, NULL, 0);
    }
  }

  esp_now_register_recv_cb(OnDataRecv);
  esp_now_register_send_cb(OnDataSent);

  pinMode(IRSENSOR_PIN, INPUT); // Set the IR sensor pin as input
}

void loop() {
  if (digitalRead(IRSENSOR_PIN) == LOW) { // Assuming LOW means detection
    lastActivityTime = millis(); // Reset the activity timer on hand detection
    lightUp(COLOR_HAND_DETECTED);
    delay(500); // Delay for visual effect
    passLightToRandomModule();
    pixels.clear(); // Turn off LEDs of the current module
    pixels.show();
  }

  if (millis() - lastActivityTime > ACTIVITY_TIMEOUT) { // Check for inactivity
    pixels.clear(); // Turn off LEDs
    pixels.show();
  }
}

void lightUp(uint32_t color) {
  for (int i = 0; i < RGBLEDNUM; i++) {
    pixels.setPixelColor(i, color); // Set color
  }
  pixels.show();
}

void passLightToRandomModule() {
  int selectedModuleIndex;
  do {
    selectedModuleIndex = random(0, sizeof(receiverAddresses) / 6); // Select a random module, excluding itself
  } while ((selectedModuleIndex + 1) == MY_ECU);

  Serial.print("Passing light to module: ");
  Serial.println(selectedModuleIndex + 1);

  esp_now_send(receiverAddresses[selectedModuleIndex], (uint8_t*)&packet, sizeof(packet));
}

void OnDataRecv(uint8_t* mac, uint8_t* incomingData, uint8_t len) {
  memcpy(&packet, incomingData, sizeof(packet));
  // Change LED color to indicate communication success
  lightUp(COLOR_COMM_SUCCESS);
}

void OnDataSent(uint8_t* mac_addr, uint8_t sendStatus) {
  Serial.print("Send status: ");
  Serial.println(sendStatus);
}

I don't understand what the game is. Do I pass levels? Accumulate points? What pods? What's the wifi for?

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

I also miss the question...

Electronic whack-a-mole over ESP-NOW on ESP8266 running WS2812

  • six moles
  • six stages are in colors (r, g, b, c, y, m)
  • decoy colors (for example: in stage "cyan" other colors are lit on other moles
  • Decrease times between color changes when stage is cleared (number of whacks registered)
1 Like

Are you sure? If you define that sensor in setup() as INPUT which, going by your picture is just a sensor and not a sensor module, wouldn't HIGH be detection? I think it's that way in a module, anyway.
I don't remember if I've ever used this one, maybe INPUT_PULLUP is what you need in setup() in order for your sensor detection logic to be true, as in someone's hand is close to the sensor and that is what you are hoping to detect?

@brooksmajor you can let the compiler calculate the number of modules:

uint8_t receiverAddresses[][6] = {
    {0x48, 0x55, 0x19, 0xED, 0xC9, 0x98}, // Module 1 MAC
    {0x48, 0x55, 0x19, 0xED, 0xAA, 0xA4}, // Module 2 MAC
    {0x48, 0x55, 0x19, 0xED, 0x0A, 0xD8}  // Module 3 MAC
};

const int nModules = sizeof receiverAddresses / sizeof *receiverAddresses;

I'm reading the code thinking about how to work with it without multiple modules or any connectivity, just to see what the logic is and see this

define MY_ECU 3 // Change for each module (1, 2, 3)

If you said "each module (0, 1, 2...) then there would be less trouble convincing myself that this

  do {
    selectedModuleIndex = random(0, sizeof(receiverAddresses) / 6); // Select a random module, excluding itself
  } while ((selectedModuleIndex + 1) == MY_ECU);

does what it looks like you are going for because you'd write this (with the constant, also shown here):

  do {
    selectedModuleIndex = random(0, nModules); // Select a random module, excluding itself
  } while (selectedModuleIndex == MY_ECU);

in its stead.

In C/C++ it is very common to number N things 0, 1, 2 … N - 1.

For reasons, but the short version is that not doing can be like swimming upstream. Like using black wire for positive, and red for negative.

If you want to take time to see more about this, Edsger W. Dijkstra, famous for way more than calling out "goto" as harmful, wrote about it back in the 20th century:

https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.