SHARE
TWEET

presepe con arduino

TringaliLuca Dec 3rd, 2017 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2.  
  3. const int fuoco = 5;          
  4. const int triggerPort = 9;
  5. const int echoPort = 8;
  6. const int cometa = 10;
  7. const int servopin = 11;
  8.  
  9. int brightness = 0;    
  10. int fadeAmount = 5;
  11. int fadePos = 5;
  12.  
  13. int maxDist = 100; //distanza massima in cm per considerare una persona vicina
  14. //NOTA: resistenza da 680 Ohm su ogni pin del segnale dei mosfet
  15.  
  16. Servo myservo;  
  17. int pos = 0;    
  18.  
  19. void setup() {
  20.     //myservo.attach(servopin);
  21.     pinMode(triggerPort, OUTPUT);
  22.     pinMode(echoPort, INPUT);
  23.     pinMode(cometa, OUTPUT);
  24.     pinMode(fuoco, OUTPUT);
  25. }
  26.  
  27. void loop() {
  28.     digitalWrite( triggerPort, LOW );
  29.     digitalWrite( triggerPort, HIGH );
  30.     delayMicroseconds( 10 );
  31.     digitalWrite( triggerPort, LOW );
  32.  
  33.     long durata = pulseIn( echoPort, HIGH );
  34.     long distanza = 0.034 * durata / 2;
  35.  
  36.     //dopo 38ms รจ fuori dalla portata del sensore
  37.     if( durata > 38000 || distanza > maxDist){
  38.         analogWrite(cometa, 1);    
  39.     } else{
  40.         int brightnessCometa = 255 - map(distanza, 0, maxDist, 1, 255);
  41.         analogWrite(cometa, brightnessCometa);    
  42.     }
  43.  
  44.     analogWrite(fuoco, brightness);    
  45.  
  46.     brightness = brightness + fadeAmount;
  47.  
  48.     if (brightness == 0 || brightness == 255) {
  49.         fadeAmount = -fadeAmount ;
  50.     }    
  51.  
  52.     myservo.write(pos);    
  53.     pos = pos + fadePos;
  54.     if (pos == 0 || pos == 180) {
  55.         fadePos = -fadePos ;
  56.     }    
  57.  
  58.     delay(100);
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top