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