Hirdetés

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

  • lanszelot

    addikt

    válasz Tomika86 #20422 üzenetére

    Először is köszönöm a segítséget mindenkinek.

    Végre megértettem, amit próbáltatok elmagyarázni.
    Rosszul értelmeztem amit írtatok. :B
    Azt hittem a loop megvárja a szünetet, mert azt hittem az úgy működik mint a delay.
    De nem, az úgy működik mint a beep.

    A loop végén írtam miert nem jó a hosszabb delay.
    Akkor az egész eltorzul.

    Viszont az elsőbe ami nem jól működik oda beszúrtam egy delay-t.
    És így, bár sufni tuning módra, de megoldódott :)

    itt a kész kód:

    #include <TimerOne.h>
    #include <Wire.h>
    #include <MultiFuncShield.h>


    int trigPin = 40; // Trigger
    int echoPin = 41; // Echo
    long duration, cm, inches;

    void setup() {
    //Serial Port begin
    Serial.begin (9600);
    //Define inputs and outputs
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    Timer1.initialize();
    MFS.initialize(&Timer1);
    }

    void loop() {
    // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Read the signal from the sensor: a HIGH pulse whose
    // duration is the time (in microseconds) from the sending
    // of the ping to the reception of its echo off of an object.
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);

    // Convert the time into a distance
    cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
    inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135

    MFS.write((int)cm);
    /*Serial.print(inches);
    Serial.print("in, ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();*/

    if (cm <= 30 && cm > 20) {
    MFS.beep(25, // beep for 250 milliseconds
    100, // silent for 1000 milliseconds
    1, // repeat above cycle 1 times
    1, // loop 1 times
    1 // wait 10 milliseconds between loop
    );
    delay(320);
    Serial.println("egyes");
    }else if (cm <= 20 && cm > 10) {
    MFS.beep(25, // beep for 250 milliseconds
    10, // silent for 100 milliseconds
    1, // repeat above cycle 1 times
    1, // loop 1 times
    1 // wait 10 milliseconds between loop
    );
    Serial.println("kettes");
    }else if (cm <= 10) {
    MFS.beep(55, // beep for 350 milliseconds
    1, // silent for 10 milliseconds
    1, // repeat above cycle 1 times
    1, // loop 1 times
    1 // wait 10 milliseconds between loop
    );
    Serial.println("harmas");
    }

    delay(320);
    }

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