]> git.piffa.net Git - sketchbook_andrea/blob - motors/simple_motor_PWM_potenziometer_transistor_diode_2/simple_motor_PWM_potenziometer_transistor_diode_2.ino
54a87ed86401bb709cbdf309073469bd5f37af50
[sketchbook_andrea] / motors / simple_motor_PWM_potenziometer_transistor_diode_2 / simple_motor_PWM_potenziometer_transistor_diode_2.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  This version uses a pot as throttle control,
9  serial for debuggin.
10  */
11
12 const int analogInPin  = A0;
13 const int motorPin     = 9;
14
15 int potValue     = 0;
16 int motValue   = 0;
17
18 void setup() {
19   pinMode(motorPin, OUTPUT);
20   Serial.begin(9600); // Debuggin
21
22 }
23 void loop() {
24   potValue = analogRead(analogInPin); 
25   motValue = potValue / 4 ; // Simple mapping 1024 -> 255
26
27   analogWrite(motorPin,motValue); // Change the PWM speed of the motor
28
29   Serial.print("Pot value = " );                       
30   Serial.print(potValue);      
31   Serial.print("\t Motor speed = ");      
32   Serial.println(motValue); 
33   delay(3); // Pause, helps to stabilize the input
34             // and keeps a brushed motor from over heating ;) 
35 }
36
37
38
39
40
41