아트인

디밍 동작 보드 소스코드

ZEROWIN.TECH 2022. 3. 7. 17:16
728x90
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>

#define USE_MEGA 1
#define USE_STATIC_IP 1

#define save_ip 96
#define save_ip2 10

#if USE_STATIC_IP

  // the dns server ip
  // the router's gateway address:
  // the subnet:
  IPAddress subnet(255, 255, 255, 0);
  //the IP address is dependent on your network
  IPAddress dnServer(192, 168, save_ip, 1);
  IPAddress gateway(192, 168, save_ip, 1);
  IPAddress ip(192, 168, save_ip, save_ip2);

#else

//   // the dns server ip
//   IPAddress dnServer(211, 9, 5, 1);
//   // the router's gateway address:
//   IPAddress gateway(211, 9, 5, 1);
//   // the subnet:
//   IPAddress subnet(255, 255, 255, 0);
//   //the IP address is dependent on your network
//   IPAddress ip(211, 9, 5, BOARD_IP);

// #endif

#endif

byte mac[] = {
  0x00, 0xAA, 0xBB, 0x76, 0x80 | save_ip2, 0xA0 
};

#define SS 10    //W5500 CS
#define RST 7    //W5500 RST
#define CS 4     //SD CS pin
const int LED = 9;


// telnet defaults to port 23
EthernetServer server(8722);

boolean alreadyConnected = false; // whether or not the client was connected previously


#define PIN_NO 3
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(PIN_NO, OUTPUT);

  #if USE_MEGA
  pinMode(SS, OUTPUT);
  pinMode(RST, OUTPUT);
  pinMode(CS, OUTPUT);
  digitalWrite(SS, HIGH);
  digitalWrite(CS, HIGH);
  /* If you want to control Reset function of W5500 Ethernet controller */
  digitalWrite(RST, HIGH);

#endif  

#if USE_STATIC_IP

   Ethernet.begin(mac, ip, dnServer, gateway, subnet);

 #else

  if(0 < save_ip) {

    IPAddress dnServer(211, 9, 6, 1);
    // the router's gateway address:
    IPAddress gateway(211, 9, 6, 1);
    // the subnet:
    IPAddress subnet(255, 255, 255, 0);
    //the IP address is dependent on your network
    IPAddress ip(211, 9, save_ip, save_ip2);

    Ethernet.begin(mac, ip, dnServer, gateway, subnet);
  }
  else {

    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // no point in carrying on, so do nothing forevermore:
      //for (;;)
      //  ;
    } 

    // max_timeout = 60;

  }



#endif

    // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();

  // start listening for clients
  server.begin();


}


#define MODE_DIMMING 

boolean bDimming = true;
int iDimming = 0;

unsigned long sendTimeStamp;

void loop() {

  if(0 < Serial.available()) {
    String str = Serial.readString();
    Serial.println(str);

    char cTempData[4];
    str.toCharArray(cTempData, 4);
    if(cTempData[0] == 'd') {
      bDimming = true;
      Serial.println("Dimming");
    }
    else {
      bDimming = false;
      
      
      //cTempData : 100
      int value = atoi(cTempData);
      analogWrite(PIN_NO, value);
      Serial.print("LED ");
      Serial.println(value);
    }
  }

  // put your main code here, to run repeatedly:
//  digitalWrite(9, true);
//  delay(1000);
//  digitalWrite(9, false);
//  delay(1000);

  

//    for(int i = 0; i <= 512; i += 1) {
//      analogWrite(PIN_NO, i);
//      delay(i < 150 ? 20 : 10);
//    }
//
//    delay(1000);
//
//    for(int i = 255; 0 <= i; i -= 1) {
//      analogWrite(PIN_NO, i);
//      delay(i < 150 ? 20 : 10);
//    }
//
//    delay(1000);

    if(bDimming == true) {
      
      iDimming += 1;
      if(512 < iDimming) iDimming = 0;

      if(iDimming == 256) {
        delay(iDimming < 150 ? 20 : 10);
      }
      else
      if(iDimming < 256) {
        analogWrite(PIN_NO, iDimming);
        delay(iDimming < 150 ? 10 : 5);
      }
      else {
        analogWrite(PIN_NO, 512 - iDimming);
        delay(512 - iDimming ? 10 : 5);
      }

//      Serial.println(iDimming);
    }

    delay(5);

    
  EthernetClient connectedClient = server.available();

    // Do we have a client?
  if (!connectedClient) {
    return;
  }

  // when the client sends the first byte, say hello:
  if (connectedClient) {
    
    if (!alreadyConnected) {
      // clead out the input buffer:
      connectedClient.flush();
      Serial.println("We have a new client");
      Serial.print("client IP address: ");
      Serial.println(connectedClient.remoteIP());
      connectedClient.println("Hello, client!");
      alreadyConnected = true;
    }

    if (connectedClient.available() > 0) {
      
      // read the bytes incoming from the client:
      String str = connectedClient.readString();

      Serial.println(str);

      char cTempData[4];
      str.toCharArray(cTempData, 4);
      if(cTempData[0] == 'd') {
        bDimming = true;
        Serial.println("Dimming");
      }
      else {
        bDimming = false;
        
        
        //cTempData : 100
        int value = atoi(cTempData);
        analogWrite(PIN_NO, value);
        Serial.print("LED ");
        Serial.println(value);
      }
      
      // echo the bytes back to the client:
      server.write(cTempData);
      // echo the bytes to the server as well:
//      Serial.write(thisChar);
    }
  }
  
    
}

'아트인' 카테고리의 다른 글

#5-3 4 채널 지연 스위치 릴레이 보드  (0) 2022.03.23
#1-4 2CH Relay + Ethernet  (0) 2022.03.22
sMilkManager 견학 제어 태블릿  (0) 2022.02.19
안드로이드 동영상 재생 박스  (0) 2022.02.18
Thingsboard 서버 기능 설정  (0) 2022.02.18