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: System Initialization
- - Source Code NOT compiled for: Arduino Nano 33 BLE
- - Source Code created on: 2025-07-05 00:25:24
- ********* 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 updateOutputs(void);
- void readInputs(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 object
- PCF8575 pcf8575(pdf_PCF8575_I2C_SLAVE_ADDRESS); // PCF8575 object
- DFRobot_ADS1115 ads(&Wire); // ADS1115 object
- /***** VARIABLES *****/
- char ipString[16]; // To store IP address string
- void setup(void)
- {
- // Initialize serial for debugging
- Serial.begin(115200);
- while (!Serial) { ; }
- // Initialize Ethernet
- pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
- digitalWrite(ethernet_W5500_RST_PIN_D2, LOW);
- delay(10);
- digitalWrite(ethernet_W5500_RST_PIN_D2, HIGH);
- delay(100);
- // Start Ethernet and get IP
- if (Ethernet.begin()) {
- IPAddress localIP = Ethernet.localIP();
- Serial.print("Ethernet initialized. IP address: ");
- Serial.println(localIP);
- // Convert IP to string
- sprintf(ipString, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
- } else {
- Serial.println("Failed to initialize Ethernet");
- }
- // Initialize SPI
- pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
- SPI.begin();
- // Initialize PCF8575
- if (pcf8575.begin()) {
- Serial.println("PCF8575 initialized");
- } else {
- Serial.println("PCF8575 initialization failed");
- }
- // Initialize ADS1115
- ads.setAddr_ADS1115(0x48); // Set address if needed
- ads.init();
- Serial.println("ADS1115 initialized");
- }
- void loop(void)
- {
- static unsigned long lastReadTime = 0;
- unsigned long currentTime = millis();
- // Read inputs every 500ms
- if (currentTime - lastReadTime >= 500) {
- readInputs();
- lastReadTime = currentTime;
- }
- // Other code can go here
- updateOutputs();
- }
- void updateOutputs()
- {
- // Set the output pin based on raw data
- digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
- }
- void readInputs()
- {
- // Read all 16 channels from PCF8575
- uint16_t inputStates = pcf8575.read16();
- // Display the input states
- Serial.print("Input states: 0x");
- Serial.println(inputStates, HEX);
- // Optionally, process individual bits
- for (uint8_t i = 0; i < 16; i++) {
- bool state = (inputStates & (1 << i)) != 0;
- Serial.print("Pin ");
- Serial.print(i);
- Serial.print(": ");
- Serial.println(state ? "HIGH" : "LOW");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement