Vous êtes sur la page 1sur 4

1:

2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:

// Controle de DOIS motores simples com LCD


#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define VALORES DOS PINOS
// Pino PWM 3 conectado base do transistor 1
int transistorPin1= 3;
int transistorPin2 = 11; // Pino PWM 11 conectado base do transistor 2
int ValorPWM1 = 0;
int ValorPWM2 = 0;
int Saida1 = 0;
int Saida2 = 0;

//armazenar o valor PWM1.


//armazenar o valor PWM1.
//armazenar o valor percentual de PWM1.
//armazenar o valor percentual de PWM1.

unsigned long changeTime; // tempo desde que o boto foi pressionado

int lcd_key
=
int adc_key_in =
#define btnRIGHT
#define btnUP
#define btnDOWN
#define btnLEFT
#define btnSELECT
#define btnNONE

0;
0;
0
1
2
3
4
5

// read the buttons


int read_LCD_buttons()
{
adc_key_in = analogRead(0);
// read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in >
if (adc_key_in <
if (adc_key_in <
if (adc_key_in <
if (adc_key_in <
if (adc_key_in <
return btnNONE;

1000) return btnNONE; // We make this the 1st option for speed r
50)
return btnRIGHT;
195) return btnUP;
380) return btnDOWN;
555) return btnLEFT;
790) return btnSELECT;
// when all others fail, return this...

}
void setup()
{
lcd.begin(16, 2);
// start the library
// lcd.setCursor(0,0);
// lcd.print("Selecione Motor"); // print a simple message
lcd.setCursor(0,0);
lcd.print("Motor 1:");
lcd.setCursor(0,1);
lcd.print("Motor 2:");
// define o pino do transistor como sada:
pinMode(transistorPin1, OUTPUT);
pinMode(transistorPin2, OUTPUT);

61:
62:
changeTime = millis();
63:
64: }
65:
66: void loop()
67: {
68:
69:
if ((millis() - changeTime) > 50)
70:
{
71:
72:
73:
74:
// lcd.setCursor(9,1);
// move cursor to second line "1" and 9 s
75:
// lcd.print(millis()/1000);
// display seconds elapsed since power-up
76:
77:
78:
lcd.setCursor(0,1);
// move to the begining of the second line
79:
lcd_key = read_LCD_buttons(); // read the buttons
80:
81:
// depending on which button was pushed, we pe
switch (lcd_key)
82:
{
83:
84:
case btnLEFT:
85:
{
86:
87:
if ( ValorPWM1 < 255)
88:
{
89:
ValorPWM1++;
90:
lcd.setCursor(10,0);
91:
lcd.print("
");
92:
Saida1 =((ValorPWM1*100/255));
93:
lcd.setCursor(10,0);
94:
lcd.print(Saida1);
95:
lcd.print("%");
96:
}
97:
else ValorPWM1=255;
98:
99:
100:
break;
101:
}
102:
103:
104:
case btnSELECT:
105:
{
106:
107:
if ( ValorPWM1 > 0)
108:
{
109:
ValorPWM1--;
110:
lcd.setCursor(10,0);
111:
lcd.print("
");
112:
Saida1 =((ValorPWM1*100/255));
113:
lcd.setCursor(10,0);
114:
lcd.print(Saida1);
115:
lcd.print("%");
116:
}
117:
else ValorPWM1=0;
118:
119:
120:
break;

121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
}
180: }

}
case btnUP:
{
if ( ValorPWM2 < 255)
{
ValorPWM2++;
lcd.setCursor(10,1);
lcd.print("
");
Saida2 =((ValorPWM2*100/255));
lcd.setCursor(10,1);
lcd.print(Saida2);
lcd.print("%");
}
else ValorPWM2=255;

break;
}
case btnDOWN:
{
if ( ValorPWM2 > 0)
{
ValorPWM2--;
lcd.setCursor(10,1);
lcd.print("
");
Saida2 =((ValorPWM2*100/255));
lcd.setCursor(10,1);
lcd.print(Saida2);
lcd.print("%");
}
else ValorPWM2=0;

break;

}
/*
case btnRIGHT:
{
lcd.print("RIGHT ");
break;
}
case btnNONE:
{
lcd.print("NONE
break;
}
*/

");

}
analogWrite(transistorPin1, ValorPWM1);
analogWrite(transistorPin2, ValorPWM2);
changeTime = millis();

181:
182:
183:
184:
185:
186:

Vous aimerez peut-être aussi