]> git.piffa.net Git - sketchbook_andrea/blob - motors/simple_motor_PWM_potenziometer__minimum_transistor_diode_3/simple_motor_PWM_potenziometer__minimum_transistor_diode_3.ino
motori e diodi
[sketchbook_andrea] / motors / simple_motor_PWM_potenziometer__minimum_transistor_diode_3 / simple_motor_PWM_potenziometer__minimum_transistor_diode_3.ino
1 /* 
2   Simple Motor : Potenziometro con minimo
3  
4  Motore DC con variazione della velocita' impostata 
5  tramite un potenziometro 10k ohms
6  
7  Schema: http://lab.piffa.net/schemi/motor_pot_bb.png
8  
9  */
10
11 const int analogInPin  = A0; // Pin a cui e' collegato il potenziometro
12 const int motorPin     = 9;
13
14 int potValue     = 0;
15 int motValue   = 0;
16 const int minMotValue = 50 ; // Valore minimo per il motore,
17             // questo variera' in base ai motori
18             
19 void setup() {
20   pinMode(motorPin, OUTPUT);
21   Serial.begin(9600); // Debuggin
22
23 }
24 void loop() {
25   potValue = analogRead(analogInPin); 
26   motValue = potValue / 4 ; // mappatura 1024 -> 255
27
28 if (motValue > minMotValue) { // Minimum motor spped check
29   analogWrite(motorPin,motValue); // Imposta la velocita' del motore
30 } else {
31 analogWrite(motorPin,LOW) ;
32 }
33
34   
35   Serial.print("Pot value = " );                       
36   Serial.print(potValue);      
37   Serial.print("\t Motore velocita' = ");      
38   Serial.println(motValue); 
39   delay(3); // Pausa, aiuta a stabilizzare l'input
40
41 }
42
43
44
45
46
47