Grüßt Euch!
Durch meine Projektarbeit bin ich an Arduino gestossen und es macht richtig Spaß, damit zu arbeiten.
Ich habe ein taktiles Ausgabegerät gebaut. Es hat mehrere Vibrationsmotoren, die dann an die Haut eine bestimmte Muster bilden.
Wie oft und wann die Motoren laufen habe ich schon programmiert. Mein Problem ist nur, dass ich einen Takt brauche. D.h. die Methoden werden alle 100 ms aufgerufen.
Ich habe dann die Methode millis() benutzt und mit Modulo gearbeitet.
In einem Array setze ich die Werte ein, wann ein Motor an(1) bzw. aus (1) ist. Mit einem Pointer gehe ich dann diesem Array entlang und prüfe die Werte.
Logisch sollte es eigentlich funktionieren, doch wenn ich mehrere Methoden aufrufe, komm ich ausserhalb von meinem vorgegeben Takt, und die Motoren laufen länger wie vorgegeben, bis der Zähler erhöht wird.
Kann mir einer von Euch sagen, wie ich das hinkriege, dass ich wirklich nur alle 100 ms die Motoren zum laufen bringe.
Den Code hänge ich mit an.
Danke im Voraus!
Lg,
Kadri
…CODE…
int motorPinB1 = 12;// defining the variable motorPinB1 as an integer and initialising it with the value 12. This will be the output 12 on Arduino-Board
int motorPinB2 = 8;
int motorPinS1 = 11;
int motorPinS2 = 10;
int motorPinS3 = 9;
int motorPinS4 = 6;
int motorPinS5 = 5;
int ledPinRed = 3;
int ledPinGreen = 2;long schritt = 100; // Sampling every 100 ms. This means here one Positin on the array.
////////-----Pattern a1-----///////////
int arrayB1 ={1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // values of motor B1; if 1 then B1 is on, else off
int arrayB2 ={0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS1 ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS2 ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS3 ={0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0};
int arrayS4 ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS5 ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};////////-----Pattern a2-----/////////// comparing pattern
int arrayB1a ={1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayB2a ={0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS1a ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS2a ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS3a ={0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0};
int arrayS4a ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int arrayS5a ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};// TODO: arraySize here
int arrayByte = sizeof(arrayB1); // Length of Bytes in an Arrayint arraySize = 12;
void setup() {
pinMode(motorPinB1, OUTPUT); // difine motorPinB1 as an output. This means on Arduino-Board the digital output 13.
pinMode(motorPinB2, OUTPUT);
pinMode(motorPinS1, OUTPUT);
pinMode(motorPinS2, OUTPUT);
pinMode(motorPinS3, OUTPUT);
pinMode(motorPinS4, OUTPUT);
pinMode(motorPinS5, OUTPUT);
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGreen, OUTPUT);Serial.begin(9600);
Serial.println(“Programm start …”);
delay(2000);// Give reader a chance to see the output.//Serial.println(arrayLength);
///////////Begin control led green////////////////
digitalWrite(ledPinGreen, HIGH); //starting signal on motorPinB1
delay(1000);
digitalWrite(ledPinGreen, LOW);
delay(2000);
///////////End control led green////////////////// Begin of pattern
playPattern( arrayB1, arrayB2, arrayS1, arrayS2, arrayS3, arrayS4, arrayS5 );
delay(2000); // wait for 2 seconds
playPattern( arrayB1a, arrayB2a, arrayS1a, arrayS2a, arrayS3a, arrayS4a, arrayS5a );///////////Begin control led red////////////////
digitalWrite(ledPinRed, HIGH); //starting signal on motorPinB1
delay(1000);
digitalWrite(ledPinRed, LOW);
delay(2000);
///////////End control led red////////////////} // ENDE setup()
void loop()
{
// void loop() is empty here, because we do not repeat anything. Nevertheless Arduino needs to have the loop() method otherwise it does´nt work.
}int nextStep(int currentPointer){
long currTime = millis();
while((millis() - currTime) < 10000){ // stop if bigger than 10 s
if (millis()% 100 ==0) { // for every “schritt” seconds : here the duration of a period ex. 100 ms
++currentPointer;
Serial.println(millis());
return currentPointer;
} // ENDE if()
} // ENDE while()} // ENDE nextStep()
void playPattern(int arrayMotorB1,
int arrayMotorB2,
int arrayMotorS1,
int arrayMotorS2,
int arrayMotorS3,
int arrayMotorS4,
int arrayMotorS5){int pointer = nextStep(-1);
while (true){// Break, if no more elements in an array
if(pointer == arraySize)
break;motor(pointer, arrayMotorB1, motorPinB1);
motor(pointer, arrayMotorB2, motorPinB2);
motor(pointer, arrayMotorS1, motorPinS1);
motor(pointer, arrayMotorS2, motorPinS2);
motor(pointer, arrayMotorS3, motorPinS3);
motor(pointer, arrayMotorS4, motorPinS4);
motor(pointer, arrayMotorS5, motorPinS5);pointer = nextStep(pointer);
} // ENDE while(pointer);
Serial.println("… ENDE des Programms!");
} // ENDE playPattern()
///////////////////////—B1—//////////////////////
void motor(int pointer, int valueArray, int motnr){Serial.print(“Nun wird Motor am Ausgang:”); Serial.print(motnr); Serial.println(" aufgerufen");
if (valueArray[pointer] == 1)
{
digitalWrite(motnr, HIGH);
}
else
{
digitalWrite(motnr, LOW);
}
}
// ENDE Programm