Hirdetés

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

  • Saua

    tag

    Sziasztok,

    Ezt a kódot sikerült összeeszkábálni, nyomógombbal és telefonról is lehet kapcsolgatni a LED –et, és a telefonon megjelenik a LED aktuális állapota.
    /*
    LED attached from pin 12 to ground
    pushbutton attached to pin 5 from +5V
    10K resistor attached to pin 5 from ground
    */
    #include <SoftwareSerial.h>

    int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
    int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3

    int led = 12;
    int button = 5;

    int dataFromBt;

    SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

    // változókat hozunk létre:
    int ledState = LOW; // kimeneti pin aktuális állapota
    int buttonState; // bemeneti pin aktuális állapota
    int lastButtonState = LOW; // bemeneti pin előző értéke

    long lastDebounceTime = 0;
    long debounceDelay = 50;

    void setup()
    {
    Serial.begin(9600); // Begin the serial monitor at 9600bps

    bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
    bluetooth.print("$"); // Print three times individually
    bluetooth.print("$");
    bluetooth.print("$"); // Enter command mode
    delay(100); // Short delay, wait for the Mate to send back CMD
    bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
    // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
    bluetooth.begin(9600); // Start bluetooth serial at 9600
    pinMode(led, OUTPUT);
    pinMode(button, INPUT);
    digitalWrite(led, ledState);
    }

    void loop()
    {

    if(bluetooth.available()) // If the bluetooth sent any characters
    {
    dataFromBt = bluetooth.read();

    if(dataFromBt == '1'){
    digitalWrite(led, HIGH);
    ledState = HIGH;

    }
    if(dataFromBt == '0'){
    digitalWrite(led, LOW);
    ledState = LOW;

    }
    if(dataFromBt == '3'){
    bluetooth.print(ledState);
    }

    }

    int reading = digitalRead(button);

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

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

    if (buttonState == HIGH) {
    ledState = !ledState;
    digitalWrite(led, ledState);
    }

    }

    }

    lastButtonState = reading;
    }

    Ezt szeretném módosítani, hogy négy LED –et lehessen kapcsolgatni.

    Főleg a LED aktuális állapotának a lekérdezésével van a gond (most a telefon 100 miliszekundomonként küldi a 3-as karaktert és visszakapja a 1 vagy 0 karaktert)…nem tudom hogyan küldhetne a többi LED más-más karaktert az állapotokról, mert az állapotuk csak LOW vagy HIGH lehet.

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