Teensy 4.1 using Ethernet Kit - conflict with LEDs (FastLED)

Status
Not open for further replies.

alskyline

Member
I have a Teensy 4.1 and Ethernet Kit that I purchased from pjrc.com. I am using the nativeEthernet.h library. I have a complex program that I am running with multiple LED Stips and Sensors. Everything works great until I uncomment ethernetSetup();. I get an IP address and the main loop begins as it should but the LEDs won't turn "ON" on any of the strips. I have used Serial.print statements to debug and everything is working correctly. Is there something that I should know about this? My code is 500+ lines and would be too much to include here. I'm hoping that someone has an easy fix. Also, are there any pins that are used by the Ethernet Kit that should not be used in my program?
 
I have tried moving the code around and trying previous versions of the libraries with no success. I changed the code that turns on the LEDs in hope that it was the fill_solid function, but there was no effect. I am also using pubSubClient.h to communicate with a MQTT Server, which is commented out right now also. My project is about finished except for this huge problem.
 
In my attempt to troubleshoot, I changed to the Ethernet.h library. The leds work as they should, but the IP address, gateway, etc won't apply. I get 0.0.0.0. for all of them. I'm using Static addresses.
 
I have attached the code here. There are two tabs:

Code:
/* New Pillar Sketch to work with Teensy 4.1
*/

#include <FastLED.h>
#include <NativeEthernet.h>
#include <PubSubClient.h>



#define PILLAR                    3

// Teensy 4.0 Stuff ================
#define LEDS_PER_SIDE             72
#define LEDS_IN_BOWL              32
#define NUMBER_OF_SIDES           4
#define NUMBER_OF_SETS_PER_SIDE   3
#define COLOR_ORDER               GRB
#define CHIPSET                   WS2811
#define FRAMES_PER_SECOND         60
#define BRIGHTNESS                255

CRGB bowl_leds[LEDS_IN_BOWL];
CRGB leds[NUMBER_OF_SIDES][LEDS_PER_SIDE];


//struct CRGB * ledarray[NUMBER_OF_SIDES][NUMBER_OF_SETS_PER_SIDE] = {{ledsHigh,ledsMid,ledsLow},{ledsHigh,ledsMid,ledsLow},{ledsHigh,ledsMid,ledsLow},{ledsHigh,ledsMid,ledsLow}};

#define NUMBER_OF_PANELS          3
#define LEDS_PER_PANEL            20
#define NUMBER_OF_BURNS           4
#define BOWL_HEIGHT               0
#define BURN_OFFSET               3   // Add this to make fire sim look better when burning at the bottom
#define GLOW_FADE_IN_RATE         2
#define GLOW_FADE_OUT_RATE        5

#define LOW_PANEL_HEIGHT          0
#define MID_PANEL_HEIGHT          26
#define HIGH_PANEL_HEIGHT         52

#define LED_STRIP_SIDE_A_PIN      19  // Data for side A LED strip
#define LED_STRIP_SIDE_B_PIN      18  // Data for side B LED strip
#define LED_STRIP_SIDE_C_PIN      14  // Data for side C LED strip
#define LED_STRIP_SIDE_D_PIN      15  // Data for side D LED strip
#define LED_STRIP_BOWL_PIN        17 // Data for bowl LED strip
//Soundboard Pins
#define SOUND_RESET_PIN           6  // Assert reset on Adafruit SFX board
#define SOUND_RX_PIN              7  // Rx for Serial2 from Adafruit SFX board
#define SOUND_TX_PIN              8  // Tx for Serial2 to Adafruit SFX board
#define SOUND_ACT_PIN             9  // Check for sound activation on Adafruit SFX board
//Sensor Pins
#define SENSOR_A1_PIN             10 // Sensor for panel 1 (top) on side A
#define SENSOR_A2_PIN             11 // Sensor for panel 2 (middle) on side A
#define SENSOR_A3_PIN             12 // Sensor for panel 3 (bottom) on side A
#define SENSOR_B1_PIN             4 // Sensor for panel 1 (top) on side B
#define SENSOR_B2_PIN             5 // Sensor for panel 2 (middle) on side B
#define SENSOR_B3_PIN             16 // Sensor for panel 3 (bottom) on side B
#define SENSOR_C1_PIN             23 // Sensor for panel 1 (top) on side C
#define SENSOR_C2_PIN             3 // Sensor for panel 2 (middle) on side C
#define SENSOR_C3_PIN             2 // Sensor for panel 3 (bottom) on side C
#define SENSOR_D1_PIN             20 // Sensor for panel 1 (top) on side D
#define SENSOR_D2_PIN             21 // Sensor for panel 2 (middle) on side D
#define SENSOR_D3_PIN             22 // Sensor for panel 3 (bottom) on side D

typedef uint8_t puzzleState_t;
int handsBrightness = 0;

enum
{
  IGNITE_LOW_A_SOUND_INDEX,
  IGNITE_LOW_B_SOUND_INDEX,
  IGNITE_LOW_C_SOUND_INDEX,
  IGNITE_LOW_D_SOUND_INDEX,
  IGNITE_MID_A_SOUND_INDEX,
  IGNITE_MID_B_SOUND_INDEX,
  IGNITE_MID_C_SOUND_INDEX,
  IGNITE_MID_D_SOUND_INDEX,
  IGNITE_HIGH_A_SOUND_INDEX,
  IGNITE_HIGH_B_SOUND_INDEX,
  IGNITE_HIGH_C_SOUND_INDEX,
  IGNITE_HIGH_D_SOUND_INDEX,
  IGNITE_BOWL_SOUND_INDEX,
  IGNITE_ALL_SOUND_INDEX,
  NUMBER_OF_IGNITE_SOUNDS,
  BURNING_SOUND_INDEX = NUMBER_OF_IGNITE_SOUNDS,
  EASTER_EGG_SOUND_INDEX,
  NUMBER_OF_SOUNDS,
};

#define MIN_SOUND_MILLIS 500UL

enum
{
  HANDPRINTS_ENABLED_STATE,
  BOWL_IGNITED_STATE,
  PILLAR_ENABLED_STATE,
  PILLAR_SOLVED_STATE,  
  EASTER_EGG_STATE
};

// Pillars numbers in room with sides shown:
// ______    _______
// |  3        4   |
// |               |
// |           C   |
// |  2      B 1 D |
// |           A   |
// ----------------
//
// Handprints (pillar, side, height*): 1B3, 2B3, 3A1
// Pillars (pillar, side, height*): 1D1, 1C2, 1C1, 1B1, 2A1, 2A2, 2B2, 2C1, 3D1, 3D3, 3C2, 3B3, 4B2, 4B3, 4A2, 4D3
// *Height: 1 = High, 2 = Mid, 3 = Low

// Map from sensor pins to positions
const struct 
{
  boolean handprint_solution;    
  boolean pillar_solution;
  uint8_t pin;
  uint16_t panel_height;
  uint8_t ignite_sound_index;
  uint8_t side_index;
  uint8_t height_index;  
} sensor_info[] =
{
  #if PILLAR == 1
  { false, false, SENSOR_A1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_A_SOUND_INDEX, 0, 2 },  
  { false, false, SENSOR_A2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_A_SOUND_INDEX,  0, 1 },  
  { false, false, SENSOR_A3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_A_SOUND_INDEX,  0, 0 },

  { false, true,  SENSOR_B1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_B_SOUND_INDEX, 1, 2 },
  { false, false, SENSOR_B2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_B_SOUND_INDEX,  1, 1 },  
  { true,  false, SENSOR_B3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_B_SOUND_INDEX,  1, 0 },
  
  { false, true,  SENSOR_C1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_C_SOUND_INDEX, 2, 2 },
  { false, true,  SENSOR_C2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_C_SOUND_INDEX,  2, 1 },  
  { false, false, SENSOR_C3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_C_SOUND_INDEX,  2, 0 },

  { false, true,  SENSOR_D1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_D_SOUND_INDEX, 3, 2 },
  { false, false, SENSOR_D2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_D_SOUND_INDEX,  3, 1 },  
  { false, false, SENSOR_D3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_D_SOUND_INDEX,  3, 0 }
  #elif PILLAR == 2
  { false, true,  SENSOR_A1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_A_SOUND_INDEX, 0, 2 },  
  { false, true,  SENSOR_A2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_A_SOUND_INDEX,  0, 1 },  
  { false, false, SENSOR_A3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_A_SOUND_INDEX,  0, 0 },

  { false, true,  SENSOR_B1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_B_SOUND_INDEX, 1, 2 },
  { false, false, SENSOR_B2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_B_SOUND_INDEX,  1, 1 },  
  { true,  false, SENSOR_B3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_B_SOUND_INDEX,  1, 0 },
  
  { false, true,  SENSOR_C1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_C_SOUND_INDEX, 2, 2 },
  { false, false, SENSOR_C2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_C_SOUND_INDEX,  2, 1 },  
  { false, false, SENSOR_C3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_C_SOUND_INDEX,  2, 0 },

  { false, false, SENSOR_D1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_D_SOUND_INDEX, 3, 2 },
  { false, false, SENSOR_D2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_D_SOUND_INDEX,  3, 1 },  
  { false, false, SENSOR_D3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_D_SOUND_INDEX,  3, 0 }
  #elif PILLAR == 3
  { true,  false, SENSOR_A1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_A_SOUND_INDEX, 0, 2 },  
  { false, false, SENSOR_A2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_A_SOUND_INDEX,  0, 1 },  
  { false, false, SENSOR_A3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_A_SOUND_INDEX,  0, 0 },

  { true, false, SENSOR_B1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_B_SOUND_INDEX, 1, 2 },
  { false, false, SENSOR_B2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_B_SOUND_INDEX,  1, 1 },  
  { false, true,  SENSOR_B3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_B_SOUND_INDEX,  1, 0 },
  
  { true, false, SENSOR_C1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_C_SOUND_INDEX, 2, 2 },
  { false, true,  SENSOR_C2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_C_SOUND_INDEX,  2, 1 },  
  { false, false, SENSOR_C3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_C_SOUND_INDEX,  2, 0 },

  { true, true,  SENSOR_D1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_D_SOUND_INDEX, 3, 2 },
  { false, false, SENSOR_D2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_D_SOUND_INDEX,  3, 1 },  
  { false, true,  SENSOR_D3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_D_SOUND_INDEX,  3, 0 }
  #elif PILLAR == 4
  { false, false, SENSOR_A1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_A_SOUND_INDEX, 0, 2 },  
  { false, true,  SENSOR_A2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_A_SOUND_INDEX,  0, 1 },  
  { false, false, SENSOR_A3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_A_SOUND_INDEX,  0, 0 },

  { false, false, SENSOR_B1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_B_SOUND_INDEX, 1, 2 },
  { false, true,  SENSOR_B2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_B_SOUND_INDEX,  1, 1 },  
  { false, true,  SENSOR_B3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_B_SOUND_INDEX,  1, 0 },
  
  { false, false, SENSOR_C1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_C_SOUND_INDEX, 2, 2 },
  { false, false, SENSOR_C2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_C_SOUND_INDEX,  2, 1 },  
  { false, false, SENSOR_C3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_C_SOUND_INDEX,  2, 0 },

  { false, false, SENSOR_D1_PIN, HIGH_PANEL_HEIGHT, IGNITE_HIGH_D_SOUND_INDEX, 3, 2 },
  { false, false, SENSOR_D2_PIN, MID_PANEL_HEIGHT,  IGNITE_MID_D_SOUND_INDEX,  3, 1 },  
  { false, true,  SENSOR_D3_PIN, LOW_PANEL_HEIGHT,  IGNITE_LOW_D_SOUND_INDEX,  3, 0 }
  #endif
};
#define NUMBER_OF_SENSORS (sizeof(sensor_info) / sizeof(sensor_info[0]))

#define IR_ACTIVATED              0
#define IR_NOT_ACTIVATED          1

#define MIN_SOUND_MILLIS 500UL
//LED Strips must use the following pins and sequence 19,18,14,15,17


static boolean sensors[NUMBER_OF_SENSORS];
static puzzleState_t puzzle_state;
puzzleState_t prev_puzzle_state;
//uint32_t current_millis = millis();



void setup() {
  Serial.begin(115200);
  ethernetSetup();
  //mqttSetup();
   
  //LED Strip Setup     
  FastLED.addLeds<NUMBER_OF_SIDES, CHIPSET, LED_STRIP_SIDE_A_PIN, COLOR_ORDER>(leds[0], LEDS_PER_SIDE);
  FastLED.addLeds<NUMBER_OF_SIDES, CHIPSET, LED_STRIP_SIDE_B_PIN, COLOR_ORDER>(leds[1], LEDS_PER_SIDE);
  FastLED.addLeds<NUMBER_OF_SIDES, CHIPSET, LED_STRIP_SIDE_C_PIN, COLOR_ORDER>(leds[2], LEDS_PER_SIDE);
  FastLED.addLeds<NUMBER_OF_SIDES, CHIPSET, LED_STRIP_SIDE_D_PIN, COLOR_ORDER>(leds[3], LEDS_PER_SIDE);
  FastLED.addLeds<NUMBER_OF_SIDES, CHIPSET, LED_STRIP_BOWL_PIN, COLOR_ORDER>(bowl_leds, LEDS_IN_BOWL);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.setMaxRefreshRate(FRAMES_PER_SECOND);
    
  // Set up general I/O

  pinMode(SOUND_ACT_PIN, INPUT_PULLUP);
  pinMode(SENSOR_C1_PIN, INPUT_PULLUP);
  pinMode(SENSOR_A1_PIN, INPUT_PULLUP);            
  pinMode(SENSOR_A2_PIN, INPUT_PULLUP);           
  pinMode(SENSOR_A3_PIN, INPUT_PULLUP);            
  pinMode(SENSOR_B1_PIN, INPUT_PULLUP);    
  pinMode(SENSOR_B2_PIN, INPUT_PULLUP);          
  pinMode(SENSOR_B3_PIN, INPUT_PULLUP);            
  pinMode(SENSOR_C1_PIN, INPUT_PULLUP);           
  pinMode(SENSOR_C2_PIN, INPUT_PULLUP);            
  pinMode(SENSOR_C3_PIN, INPUT_PULLUP);            
  pinMode(SENSOR_D1_PIN, INPUT_PULLUP);            
  pinMode(SENSOR_D2_PIN, INPUT_PULLUP);            
  pinMode(SENSOR_D3_PIN, INPUT_PULLUP);

  // Set up debug serial and sound card serial
  
  
 Serial.println("Testing Mode - Pillars4.1");
 

}
void loop() {
  uint16_t i;
  uint16_t j;
  uint8_t side;
  
  // Read the sensors and publish to GameMaster Software
  for( i = 0; i < NUMBER_OF_SENSORS; i++ )
    {
    sensors[i] = digitalRead(sensor_info[i].pin);
    Serial.print(sensors[i]); //Sensor Data to Print or Publish
    Serial.print(" ");
    }  
  Serial.println();
  // Update the puzzle state
  prev_puzzle_state = puzzle_state;
  //updatePuzzleState(&puzzle_state, current_millis);

  // Lighting and Sound for Hands
  if(puzzle_state == HANDPRINTS_ENABLED_STATE)
  {
    for( i = 0; i< NUMBER_OF_SENSORS; i++)
        {
          if(sensor_info[i].handprint_solution)
          {
            if(sensors[i] == IR_ACTIVATED)
            {
                Serial.println("ON");
                 fill_solid(leds[sensor_info[i].side_index] + sensor_info[i].panel_height,LEDS_PER_PANEL, CRGB::Green);
              
              
            }
            else {
                Serial.println("OFF");
                 fill_solid(leds[sensor_info[i].side_index] + sensor_info[i].panel_height,LEDS_PER_PANEL, CRGB::Black);
                 FastLED.show(); 
              }
              
              
              
            
        }
   }
   
   FastLED.show();
  } //End of Handprints

  //Send & Recieve Messages
  //mqttLoop();
  FastLED.show();
}

Second Tab:
Code:
//Arduino Ethernet MQTT
int resetStatus = 0;
int overrideStatus = 0;
int solvedStatus = 0;


// Device Addresses
// MAC address of this device (Use MAC Address Generator app)
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x4C};
// Unique IP address assigned to device

const IPAddress ip(192, 168, 0, 205);
// Gateway Address
const IPAddress gateway(192, 168, 0, 1);
//Subnet Mask
const IPAddress subnet(255, 255, 255, 0);
//DNS Server Address
const IPAddress dnServer(192, 168, 0, 1);
// IP address of the machine running the MQTT server 
const IPAddress mqttServerIP(192, 168, 0, 200);
// Unique name of this device, used as client ID and Topic Name on MQTT
const char* deviceID = "PillarThree";

// Global Variables 
// Create an instance of the Ethernet client
EthernetClient ethernetClient;
// Create an instance of the MQTT client based on the ethernet client
PubSubClient MQTTclient(ethernetClient);
// The time (from millis()) at which last message was published
long lastMsgTime = 0;
// A buffer to hold messages to be sent/have been received
char msg[64];
// The topic in which to publish a message
char topic[32];
// Counter for number of heartbeat pulses sent
int pulseCount = 0;

// Callback function each time a message is published in any of
// the topics to which this client is subscribed
void mqttCallback(char* topic, byte* payload, unsigned int length) {

  // The message "payload" passed to this function is a byte*
  // Let's first copy its contents to the msg char[] array
  memcpy(msg, payload, length);
  // Add a NULL terminator to the message to make it a correct string
  msg[length] = '\0';

  // Debug
  Serial.print("Message received in topic [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println(msg);

  // Act upon the message received

  if(strcmp(msg, "solveOne") == 0) {
    solvedStatus = 1; //Phrase was said correctly
  }
  else if(strcmp(msg, "resetOne") == 0) {
    resetStatus = 1; //HANDPRINTS_ENABLED_STATE
    
  }
  else if(strcmp(msg, "overrideOne") == 0) {
    overrideStatus = 1; //Hands Override
    Serial.println("OverrideOne message recieved");
  }
  else if(strcmp(msg, "overrideTwo") == 0) {
    overrideStatus = 2; //Pillars Override
  }
  else if(strcmp(msg, "ResetTwo") == 0) {
    resetStatus = 2; //Reset to Pillar Enabled State
  }
}

void ethernetSetup() { 
  // start the Ethernet connection:

  Serial.println("Initialize Ethernet:");
  Ethernet.begin(mac, ip);
  delay(1500);
 
  
  // print your local IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  delay(1000);
}

void mqttSetup() {
  // Define some settings for the MQTT client
  MQTTclient.setServer(mqttServerIP, 1883);
  MQTTclient.setCallback(mqttCallback);
}

void mqttLoop() {
  // Ensure there's a connection to the MQTT server
  while (!MQTTclient.connected()) {
  
    // Debug info
    Serial.print("Attempting to connect to MQTT broker at ");
    Serial.println(mqttServerIP);

    // Attempt to connect
    if (MQTTclient.connect(deviceID)) {
    
      // Debug info
      Serial.println("Connected to MQTT broker");
      
      // Once connected, publish an announcement to the ToHost/#deviceID# topic

      snprintf(topic, 32, "ToHost/Pillars/Three", deviceID);
      snprintf(msg, 64, "CONNECTED", deviceID);
      MQTTclient.publish(topic, msg);
      
      // Subscribe to topics meant for this device
      snprintf(topic, 32, "ToDevice/Pillars/Three", deviceID);
      
      MQTTclient.subscribe(topic);
      
      // Subscribe to topics meant for all devices
      MQTTclient.subscribe("ToDevice/All");
    }
    else {
      // Debug info why connection could not be made
      Serial.print("Connection to MQTT server failed, rc=");
      Serial.print(MQTTclient.state());
      Serial.println(" trying again in 5 seconds");
      Serial.println(Ethernet.localIP());
      Serial.println(Ethernet.hardwareStatus());
      
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
  
  // Call the main loop to check for and publish messages
  MQTTclient.loop();
}


void publish(char* message){

  snprintf(topic, 32, "ToHost/Pillars/Three", deviceID);
  MQTTclient.publish(topic, message);
}
 
Status
Not open for further replies.
Back
Top