Paso 21: codificación
Copiar el código:
Este esbozo consiste en ejecutar un sensor PIR, 4 luces parpadeantes y un pequeño motor DC. Sensor PIR
dispara haciendo que las luces LED flash y luego encienda un motor
definir los pines
#define LEDPINFAST1 7, //LED luz
#define LEDPINFAST2 8; //LED luz
#define LEDPINSLOW1 10; //LED luz
#define LEDPINSLOW2 9; //LED luz
fast1 int = LEDPINFAST1; //LED luz parpadea rápido
fast2 int = LEDPINFAST2; //LED luz parpadea rápido
slow1 int = LEDPINSLOW1; //LED luz parpadeo lento
slow2 int = LEDPINSLOW2; //LED luz parpadeo lento
int calibrationTime = 30; / / calibrar el sensor de PIR
el momento cuando el sensor produce un impulso de baja
tiempo unsigned int lowIn;
la cantidad de milisegundos que el sensor tiene que ser bajo
antes de asumir todo el movimiento se ha detenido
pausa larga unsigned int = 100;
Boolean lockLow = true;
takeLowTime Boolean;
pirPin int = 2; el pin digital conectado a la salida del sensor PIR
motorPin int = 11; Motor pin conectado
/////////////////////////////
PROGRAMA DE INSTALACIÓN
void setup() {}
Serial.Begin(9600);
pinMode (pirPin, entrada);
pinMode (motorPin, salida);
digitalWrite (pirPin, LOW);
pinMode(fast1,OUTPUT);
pinMode(fast2,OUTPUT);
pinMode(slow1,OUTPUT);
pinMode(slow2,OUTPUT);
sensor a calibrar escriba a programa Aduino
Serial.Print ("calibración sensor");
para (int i = 0; i < calibrationTime; i ++) {}
Serial.Print(".");
Delay(1000);
}
Serial.println ("hecho");
Serial.println ("SENSOR activo");
Delay(50);
}
////////////////////////////
LAZO
void loop() {//I sé que hay mejores formas de escribir esta cadena de irratic que destella luces - pero todavía estoy aprendiendo
if(digitalRead(pirPin) == HIGH) {}
analogWrite (slow2, 100);
Delay(1000);
analogWrite (slow2, LOW);
Delay(10); {
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(10);
digitalWrite (slow1, LOW);
Delay(10);
digitalWrite (fast1, HIGH);
Delay(100);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(10);
digitalWrite (slow1, LOW);
Delay(30);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(2000);
digitalWrite (slow1, LOW);
Delay(10);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(10);
digitalWrite (slow1, LOW);
Delay(30);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(500);
digitalWrite (slow1, LOW);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(2000);
digitalWrite (slow1, LOW);
digitalWrite (fast1, HIGH);
Delay(100);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(10);
digitalWrite (slow1, LOW);
Delay(30);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(2000);
digitalWrite (slow1, LOW);
Delay(10);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(10);
digitalWrite (slow1, LOW);
Delay(30);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(500);
digitalWrite (slow1, LOW);
digitalWrite (fast1, HIGH);
Delay(10);
digitalWrite (fast1, LOW);
Delay(10);
digitalWrite (fast2, HIGH);
Delay(10);
digitalWrite (fast2, LOW);
Delay(10);
digitalWrite (slow1, HIGH);
Delay(2000);
digitalWrite (slow1, LOW);
analogWrite (slow2, 10);
Delay(5);
analogWrite (slow2, 50); //LED luz a tener medio brillo
Delay(10);
analogWrite (slow2, 100);
}
digitalWrite (motorPin, HIGH); {pin motor //the es en
Delay(1000); Espere un segundo
digitalWrite (motorPin, LOW); el motor está apagado
Delay(5000); espere durante cinco segundos
analogWrite (slow2, LOW); //LED de la luz de
}
if(lockLow) {//back al sensor
PIR baja antes de la salida de más:
lockLow = false;
Serial.println("---");
Serial.Print ("movimiento detectado en");
Serial.Print(Millis()/100);
Serial.println ("sec");
Delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW) {}
digitalWrite (motorPin, LOW); PIR es baja entonces el motor es baja
{if(takeLowTime)}
lowIn = millis(); transición de alto a bajo
takeLowTime = false;
}
Si (! lockLow & & millis() - lowIn > pausa) {}
hace que este bloque de código es sólo se ejecuta después de
se ha detectado una nueva secuencia de movimiento
lockLow = true;
Serial.Print ("movimiento terminada en"); salida
Serial.Print((Millis() - pausa) / 100);
Serial.println ("sec");
Delay(50);
}
}
}