Új hozzászólás Aktív témák

  • Saua

    tag

    Sziasztok,

    Eredetileg egy LED-et lehet ki/be kapcsolni neten keresztül a ”Led1” utasítással. Ezt módosítottam, hogy egy nyomógombbal is működjön. A probléma, hogy a weboldara nem sikerül kiíratni a LED állapotát ha a gombbal kapcsolgatom.

    Hogyan kellene módosítani a kódot?

    /*
    LED attached from pin 6 to ground
    pushbutton attached to pin 2 from +5V
    10K resistor attached to pin 2 from ground
    */
    #include <SPI.h>
    #include <Ethernet.h>
    #include <String.h>

    byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
    byte ip[] = { 192, 168, 1, 202 };
    EthernetServer server(80);

    int led1 = 6;
    int button = 2;
    String readString = String(30) ;
    boolean statusLed = false;
    int buttonState; // bemeneti pin aktuális állapota
    int lastButtonState = LOW; // bemeneti pin előző értéke

    long lastDebounceTime = 0;
    long debounceDelay = 50;
    long prevido1; //előző idő eltárolására


    void setup() {

    pinMode(led1, OUTPUT);
    pinMode(button, INPUT);
    Serial.begin(9600);
    Ethernet.begin(mac, ip);
    }

    void loop() {

    EthernetClient client = server.available(); // Create a client connection

    if (client) {
    while (client.connected())
    {
    if (client.available ())
    {
    char c = client.read(); // Read char by char HTTP request
    if (readString.length() < 30)
    {
    readString += (c) ;
    }

    if (c == '\n')
    {
    if(readString.indexOf("Led1")>=0)
    {
    if (statusLed) {
    digitalWrite (led1, LOW) ;
    statusLed = false;
    } else {
    digitalWrite (led1, HIGH) ;
    statusLed = true;
    }
    }

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println();

    client.print ("<font size ='20'>");
    if (statusLed) {
    client.print("Led1 bekapcsolva");
    } else {
    client.print("Led1 kikapcsolva");
    }
    readString="";
    delay(1); // give the web browser time to receive the data
    client.stop();
    }

    }

    }
    }
    //==============================
    int reading = digitalRead(button);

    if (reading != lastButtonState) {
    lastDebounceTime = millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
    buttonState = reading;

    if (buttonState == HIGH) {
    statusLed = !statusLed;
    digitalWrite(led1, statusLed);
    }
    }
    }

    lastButtonState = reading;
    //==============================
    }

Új hozzászólás Aktív témák