Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NUMLED 4
- #define NUMBOT 4
- #define HOR 0
- #define ANTI 1
- #define MAIS 2
- #define MENOS 3
- #define LIGADO LOW
- #define DESLIGADO HIGH
- #define APERTADO LOW
- #define NAPERTADO HIGH
- #define TEMPOMAX 2000
- long int time;
- int tecla = 0;
- int vetLed[NUMLED] = {6, 7, 8, 9};
- int vetBot[NUMBOT] = {2, 3, 4, 5};
- int curLed = 0;
- int tempo = 500;
- int sentido;
- int passo = 50;
- void setup() {
- Serial.begin(19200);
- for(int i = 0; i < NUMLED; i++){
- pinMode(vetLed[i], OUTPUT);
- pinMode(vetBot[i], INPUT);
- }
- }
- void loop() {
- ligaLed();
- //for(int i = 0; i < tempo; i++){
- while((millis() - time) < tempo){
- checaTec();
- checaBot();
- //delay(1);
- }
- desligaLed();
- giraLed();
- }
- void ligaLed (){
- digitalWrite(vetLed[curLed], LIGADO);
- time = millis();
- }
- void desligaLed (){
- digitalWrite(vetLed[curLed], DESLIGADO);
- }
- void giraLed (){
- if(sentido == ANTI){
- curLed++;
- if(curLed >= NUMLED)
- curLed = 0;
- }else{
- if(sentido == HOR){
- curLed--;
- if(curLed < 0)
- curLed = NUMLED - 1;
- }
- }
- }
- void checaTec(){
- if(!Serial.available())
- return;
- tecla = Serial.read();
- switch(tecla){
- case 'a':
- case 'A':
- sentido = HOR;
- if(tecla == 'h') break;
- break;
- case 'h':
- case 'H':
- sentido = ANTI;
- if(tecla == 'a') break;
- break;
- case '+':
- case '=':
- tempo = tempo - passo;
- if(tempo <=50) tempo = passo;
- break;
- case '-':
- case '_':
- tempo = tempo + passo;
- if(tempo >= 2000) tempo = 2000;
- break;
- }
- }
- void checaBot(){
- for(int i = 0; i < NUMBOT; i++){
- if(digitalRead(vetBot[i]) == APERTADO){
- switch(i){
- case HOR: sentido = HOR;
- break;
- case ANTI: sentido = ANTI;
- break;
- case MAIS:
- tempo = tempo - passo;
- if(tempo <= passo) tempo = passo;
- break;
- case MENOS:
- tempo = tempo + passo;
- if(tempo >= TEMPOMAX) tempo = TEMPOMAX;
- break;
- }
- while(digitalRead(vetBot[i]) == APERTADO);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement