]> 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
83965d143790493f98bd973d2cc4eaa9d35bfff9
[sketchbook_andrea] / motors / simple_motor_PWM_potenziometer__minimum_transistor_diode_3 / simple_motor_PWM_potenziometer__minimum_transistor_diode_3.ino
1 /* 
2  Simple Motor with variable spped from pot input
3  
4  This sketch use a transistor and a diode
5  in order to poer a 5v ~150mAh directly from the board.
6  Hard thing is to find a suitable motor!
7
8
9   This version uses a pot as throttle control,
10  serial for debuggin.
11
12  Optimized for a minimum throttle value in order to keep the motor off
13  */
14
15 const int analogInPin  = A0;
16 const int motorPin     = 9;
17
18 int potValue     = 0;
19 int motValue   = 0;
20 const int minMotValue = 50 ; // Minimum power requiement of my own motr,
21             // This is going to differ from motor to motor
22             
23 void setup() {
24   pinMode(motorPin, OUTPUT);
25   Serial.begin(9600); // Debuggin
26
27 }
28 void loop() {
29   potValue = analogRead(analogInPin); 
30   motValue = potValue / 4 ; // Simple mapping 1024 -> 255
31
32 if (motValue > minMotValue) { // Minimum motor spped check
33   analogWrite(motorPin,motValue); // Change the PWM speed of the motor
34 } else {
35 analogWrite(motorPin,LOW) ;
36 }
37
38   
39   Serial.print("Pot value = " );                       
40   Serial.print(potValue);      
41   Serial.print("\t Motor speed = ");      
42   Serial.println(motValue); 
43   delay(3); // Pause, helps to stabilize the input, 
44             // slows the serial output
45 }
46
47
48
49
50
51