Hi,
muss für mein Schulprojekt einen Aufzug bauen und mit Arduino programmieren. Er soll immer auf der gleichen Höhe bleiben, wenn er z.B. manuell verschoben wird. Genutzt wird ein gewöhnlicher Ultraschallsensor, Arduino UNO, Arduino Motor Shield Rev3 und zwei Motoren. Ich habe ein Problem mit meinem Code. Der Ultraschallsensor gibt beim ersten Mal einen Wert aus, danach kommt immer nur der Wert 0, egal was passiert... Um zu checken, wo das Problem liegt, habe ich ein Serial.Print eingefügt. Das zeigt das eben beschriebene. Weiß jemand, wo das Problem liegt? Da ich kein Profi bin, kann der Fehler auch recht banal sein... :)
Bei der Hardware liegt kein Problem, das ist sicher!
Hier der Code:
/**set control port**/
const int E1Pin = 6;
const int M1Pin = 7;
const int E2Pin = 11;
const int M2Pin = 13;
/**inner definition**/
typedef struct {
byte enPin;
byte directionPin;
} MotorContrl;
const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;
const MotorContrl MotorPin[] = { {E1Pin, M1Pin}, {E2Pin, M2Pin} } ;
const int Forward = LOW;
const int Backward = HIGH;
float Schallgeschwindigkeit=0.03432;
int Trigger=6;
int Echo=7;
int Idealabstand=4;
long Dauer=0;
long Entfernung=0;
void setup()
{
Serial.begin (9600);
pinMode(Trigger, OUTPUT);
pinMode(Echo, INPUT);
}
/**functions**/
void initMotor( ) {
int i;
for ( i = 0; i < MotorNum; i++ ) {
digitalWrite(MotorPin[i].enPin, LOW);
pinMode(MotorPin[i].enPin, OUTPUT);
pinMode(MotorPin[i].directionPin, OUTPUT);
}
}
void setMotorDirection( int motorNumber, int direction ) {
digitalWrite( MotorPin[motorNumber].directionPin, direction);
}
inline void setMotorSpeed( int motorNumber, int speed ) {
analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed / 100.0) ); //PWM
}
void MotorHoch(){
initMotor();
setMotorDirection( M1, Backward );
setMotorSpeed( M1, 100 );
delay(1000);
setMotorSpeed( M1, 0 );
delay(100);
Serial.println("zu niedrig - Motor Hoch");
setMotorSpeed( M2, 0 );
setMotorDirection( M2, Backward );
for (int value = 0 ; value <= 100; value += 5) {
setMotorSpeed( M2, value );
delay(100);
}
setMotorSpeed( M2, 0 );
}
void MotorRunter(){
initMotor();
setMotorDirection( M1, Forward );
setMotorSpeed( M1, 100 );
delay(1000);
setMotorSpeed( M1, 0 );
delay(100);
Serial.println("zu hoch - Motor Runter");
setMotorSpeed( M2, 0 );
setMotorDirection( M2, Forward );
for (int value = 0 ; value <= 100; value += 5) {
setMotorSpeed( M2, value );
delay(100);
}
setMotorSpeed( M2, 0 );
}
void loop(){
digitalWrite(Trigger, LOW);
delay(5);
digitalWrite(Trigger, HIGH);
delay(10);
digitalWrite(Trigger, LOW);
Dauer = pulseIn(Echo, HIGH);
Entfernung = (Dauer/2) * Schallgeschwindigkeit;
Serial.print("Entfernung:");
Serial.println(Dauer);
if (Entfernung >= Idealabstand) {
MotorRunter();
}
else{
if (Entfernung <= Idealabstand) {
MotorHoch();
}
}
delay(100);
OK danke. Ich bin halt noch ein Anfänger.
Und dürfte ich fragen, was du mit Typ meinst?