Advertisement
pleasedontcode

System Initialization rev_08

Jul 4th, 2025
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: System Initialization
  13.     - Source Code NOT compiled for: Arduino Nano 33 BLE
  14.     - Source Code created on: 2025-07-05 00:25:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Detect and initialize the Ethernet connection */
  21.     /* using the Ethernet2 library and the W5500 module. */
  22.     /* Retrieve and display the assigned IP address to */
  23.     /* confirm successful connection. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* Monitor and read all channels connected via the */
  26.     /* PCF8575 I2C expander periodically, ensuring real- */
  27.     /* time data acquisition from the digital inputs. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <SPI.h>
  34. #include <Wire.h>
  35. #include <Ethernet2.h>    // https://github.com/adafruit/Ethernet2
  36. #include <PCF8575.h>      // https://github.com/RobTillaart/PCF8575.git
  37. #include <DFRobot_ADS1115.h> // https://github.com/DFRobot/DFRobot_ADS1115
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42. void updateOutputs(void);
  43. void readInputs(void);
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t ethernet_W5500_RST_PIN_D2 = 2;
  47.  
  48. /***** DEFINITION OF I2C PINS *****/
  49. const uint8_t pdf_PCF8575_I2C_PIN_SDA_A4 = A4;
  50. const uint8_t pdf_PCF8575_I2C_PIN_SCL_A5 = A5;
  51. const uint8_t pdf_PCF8575_I2C_SLAVE_ADDRESS = 0x20; // 32 decimal
  52. const uint8_t adc_ADS1115_I2C_SLAVE_ADDRESS = 0x48; // 72 decimal
  53.  
  54. /***** DEFINITION OF SPI PINS *****/
  55. const uint8_t ethernet_W5500_SPI_PIN_MOSI_D11 = 11;
  56. const uint8_t ethernet_W5500_SPI_PIN_MISO_D12 = 12;
  57. const uint8_t ethernet_W5500_SPI_PIN_SCLK_D13 = 13;
  58. const uint8_t ethernet_W5500_SPI_PIN_CS_D10 = 10;
  59.  
  60. /***** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  61. Ethernet2 Ethernet; // Ethernet object
  62. PCF8575 pcf8575(pdf_PCF8575_I2C_SLAVE_ADDRESS); // PCF8575 object
  63. DFRobot_ADS1115 ads(&Wire); // ADS1115 object
  64.  
  65. /***** VARIABLES *****/
  66. char ipString[16]; // To store IP address string
  67.  
  68. void setup(void)
  69. {
  70.   // Initialize serial for debugging
  71.   Serial.begin(115200);
  72.   while (!Serial) { ; }
  73.  
  74.   // Initialize Ethernet
  75.   pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
  76.   digitalWrite(ethernet_W5500_RST_PIN_D2, LOW);
  77.   delay(10);
  78.   digitalWrite(ethernet_W5500_RST_PIN_D2, HIGH);
  79.   delay(100);
  80.  
  81.   // Start Ethernet and get IP
  82.   if (Ethernet.begin()) {
  83.     IPAddress localIP = Ethernet.localIP();
  84.     Serial.print("Ethernet initialized. IP address: ");
  85.     Serial.println(localIP);
  86.     // Convert IP to string
  87.     sprintf(ipString, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
  88.   } else {
  89.     Serial.println("Failed to initialize Ethernet");
  90.   }
  91.  
  92.   // Initialize SPI
  93.   pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
  94.   SPI.begin();
  95.  
  96.   // Initialize PCF8575
  97.   if (pcf8575.begin()) {
  98.     Serial.println("PCF8575 initialized");
  99.   } else {
  100.     Serial.println("PCF8575 initialization failed");
  101.   }
  102.  
  103.   // Initialize ADS1115
  104.   ads.setAddr_ADS1115(0x48); // Set address if needed
  105.   ads.init();
  106.   Serial.println("ADS1115 initialized");
  107. }
  108.  
  109. void loop(void)
  110. {
  111.   static unsigned long lastReadTime = 0;
  112.   unsigned long currentTime = millis();
  113.  
  114.   // Read inputs every 500ms
  115.   if (currentTime - lastReadTime >= 500) {
  116.     readInputs();
  117.     lastReadTime = currentTime;
  118.   }
  119.  
  120.   // Other code can go here
  121.   updateOutputs();
  122. }
  123.  
  124. void updateOutputs()
  125. {
  126.   // Set the output pin based on raw data
  127.   digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
  128. }
  129.  
  130. void readInputs()
  131. {
  132.   // Read all 16 channels from PCF8575
  133.   uint16_t inputStates = pcf8575.read16();
  134.  
  135.   // Display the input states
  136.   Serial.print("Input states: 0x");
  137.   Serial.println(inputStates, HEX);
  138.  
  139.   // Optionally, process individual bits
  140.   for (uint8_t i = 0; i < 16; i++) {
  141.     bool state = (inputStates & (1 << i)) != 0;
  142.     Serial.print("Pin ");
  143.     Serial.print(i);
  144.     Serial.print(": ");
  145.     Serial.println(state ? "HIGH" : "LOW");
  146.   }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement