Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Network Configuration
- - Source Code NOT compiled for: Arduino Nano 33 BLE
- - Source Code created on: 2025-07-05 00:32:34
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Detect and initialize the Ethernet connection */
- /* using the Ethernet2 library and the W5500 module. */
- /* Retrieve and display the assigned IP address to */
- /* confirm successful connection. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* Monitor and read all channels connected via the */
- /* PCF8575 I2C expander periodically, ensuring real- */
- /* time data acquisition from the digital inputs. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <Wire.h>
- #include <Ethernet2.h> // https://github.com/adafruit/Ethernet2
- #include <PCF8575.h> // https://github.com/RobTillaart/PCF8575.git
- #include <DFRobot_ADS1115.h> // https://github.com/DFRobot/DFRobot_ADS1115
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void monitorAndReadChannels(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t ethernet_W5500_RST_PIN_D2 = 2;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t pdf_PCF8575_I2C_PIN_SDA_A4 = A4;
- const uint8_t pdf_PCF8575_I2C_PIN_SCL_A5 = A5;
- const uint8_t pdf_PCF8575_I2C_SLAVE_ADDRESS = 0x20; // 32 decimal
- const uint8_t adc_ADS1115_I2C_SLAVE_ADDRESS = 0x48; // 72 decimal
- /***** DEFINITION OF SPI PINS *****/
- const uint8_t ethernet_W5500_SPI_PIN_MOSI_D11 = 11;
- const uint8_t ethernet_W5500_SPI_PIN_MISO_D12 = 12;
- const uint8_t ethernet_W5500_SPI_PIN_SCLK_D13 = 13;
- const uint8_t ethernet_W5500_SPI_PIN_CS_D10 = 10;
- /***** DEFINITION OF LIBRARY CLASS INSTANCES *****/
- Ethernet2 Ethernet; // Ethernet instance
- PCF8575 pcf8575(pdf_PCF8575_I2C_SLAVE_ADDRESS); // PCF8575 instance
- DFRobot_ADS1115 ads1115(&Wire); // ADS1115 instance
- /***** VARIABLES *****/
- // Raw data and physical data for the W5500 reset pin
- bool ethernet_W5500_RST_PIN_D2_rawData = false;
- // Timing variables for periodic monitoring
- unsigned long previousMonitorMillis = 0;
- const unsigned long monitorInterval = 1000; // 1 second
- void setup(void)
- {
- // Initialize serial for debugging
- Serial.begin(115200);
- while (!Serial); // Wait for serial port
- // Initialize reset pin
- pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
- // Initialize SPI
- pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
- SPI.begin();
- // Reset W5500 module
- digitalWrite(ethernet_W5500_RST_PIN_D2, LOW);
- delay(10);
- digitalWrite(ethernet_W5500_RST_PIN_D2, HIGH);
- delay(100);
- // Detect and initialize Ethernet connection
- Serial.println("Initializing Ethernet...");
- if (Ethernet.begin()) {
- Serial.println("Ethernet initialized successfully.");
- } else {
- Serial.println("Ethernet initialization failed.");
- }
- // Retrieve and display IP address
- IPAddress ip = Ethernet.localIP();
- Serial.print("Assigned IP Address: ");
- Serial.println(ip);
- // Initialize PCF8575
- if (pcf8575.begin()) {
- Serial.println("PCF8575 initialized successfully.");
- } else {
- Serial.println("PCF8575 initialization failed.");
- }
- // Initialize ADS1115
- ads1115.init();
- if (ads1115.checkADS1115()) {
- Serial.println("ADS1115 initialized successfully.");
- } else {
- Serial.println("ADS1115 initialization failed.");
- }
- }
- void loop(void)
- {
- unsigned long currentMillis = millis();
- // Periodically monitor and read all channels
- if (currentMillis - previousMonitorMillis >= monitorInterval)
- {
- previousMonitorMillis = currentMillis;
- monitorAndReadChannels();
- }
- // Refresh output data
- updateOutputs();
- }
- void monitorAndReadChannels()
- {
- // Read all 16 channels from PCF8575
- uint16_t channelStates = pcf8575.read16();
- // Display the states
- Serial.print("PCF8575 Channel States: 0x");
- Serial.println(channelStates, HEX);
- // Optional: process individual pins if needed
- for (uint8_t pin = 0; pin < 16; pin++)
- {
- uint8_t pinState = (channelStates & (1 << pin)) ? HIGH : LOW;
- Serial.print("Pin ");
- Serial.print(pin);
- Serial.print(": ");
- Serial.println(pinState == HIGH ? "HIGH" : "LOW");
- }
- }
- void updateOutputs()
- {
- // Set the W5500 reset pin according to rawData
- digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement