Ferramentas do usuário

Ferramentas do site


cursos:introrobotica:2019-1:grupo04:tp4.c
#include "controladores.h"
#include "sensores.h"
#include "funcoes.h"
 
const unsigned char PS_16 = (1 << ADPS2);
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
 
 
 
int optDir = 15; 
int optEsq = 14;
int optFrente = 11;
int estadoDir = 0; 
int estadoEsq = 0;
int mediaOpt = 800;
int mediaBranco = 0;
int mediaPreto = 0;
 
 
 
void calibraOptico(){  
  lcd.setCursor(0,0);  
  lcd.print("Calibrando Branco Dir");
  delay(2000);
  for(int i=0;i<20;i++){
    estadoDir = analogRead(optDir);
    mediaBranco = mediaBranco + estadoDir;
    i++;
  }
  mediaBranco = mediaBranco/20;
  Serial.println(mediaBranco);
  lcd.setCursor(0,0);  
  lcd.print("Calibrando Branco Esq");
  delay(2000);
 
  for(int i=0;i<20;i++){
    estadoDir = analogRead(optEsq);
    mediaPreto = mediaPreto + estadoDir;
    i++;
  }
  mediaPreto = mediaPreto/20;
  mediaOpt = (mediaPreto+mediaBranco)/2;
  Serial.println(mediaOpt);
  lcd.setCursor(0,0);  
  lcd.print("Linha          ");
  delay(6000);
 
}
 
void espera(){
  int sensor;
  sensor = analogRead(optDir);
while( sensor>200){
  sensor = analogRead(optDir);
 
}
}
 
void segLinha(){
 
  while(t_atual < t_start + 60000){
    estadoDir = analogRead(optDir);  //Lê o valor fornecido pelo LDR 
    estadoEsq = analogRead(optEsq);  //Lê o valor fornecido pelo LDR
    motor1->setSpeed(vel1);
    motor2->setSpeed(vel2);
 
    if ((estadoDir<mediaOpt)&&(estadoEsq<mediaOpt)){
        motor1->run(FORWARD);
        motor2->run(FORWARD);
        delay(100);
    }
    if ((estadoDir>mediaOpt)&&(estadoEsq<mediaOpt)){
 
      motor1->run(FORWARD);
      motor2->setSpeed(80);
      motor2->run(BACKWARD);
      delay(100);
 
    }
    if ((estadoDir<mediaOpt)&&(estadoEsq>mediaOpt)){
      motor1->setSpeed(50);
      motor1->run(BACKWARD);
      motor2->run(FORWARD);
      delay(100);
    } 
     if ((estadoDir>mediaOpt)&&(estadoEsq>mediaOpt)){
        motor1->run(BACKWARD);
        motor2->run(FORWARD);
        delay(100);
 
    }
    if (analogRead(optFrente)<60){
        motor1->run(RELEASE);
        motor2->run(RELEASE);
        delay(500);
        int checacor = checaCores();
        if(checacor == 1){
          re(0.2);
          gira_esq(30);
          break;
        }
        else {      
          ldr();
          frente(0.4);
          break;
        }
 
    }
  if (t_atual > (t_start +60000)){
  break;
  }
  t_atual = millis();
 
  }
  motor1->run(RELEASE);
  motor2->run(RELEASE);
}
 
 
void setup() {
  lcd.begin(16, 2);    
  Serial.begin(9600);
   // configura o preescaler do ADC
 ADCSRA &= ~PS_128;  //limpa configuração da biblioteca do arduino
 
 // valores possiveis de prescaler só deixar a linha com prescaler desejado
 // PS_16, PS_32, PS_64 or PS_128
 //ADCSRA |= PS_128; // 128 prescaler
    ADCSRA |= PS_64; // 64 prescaler
 // ADCSRA |= PS_32; // 32 prescaler
  //ADCSRA |= PS_16; // 16 prescaler
  AFMS.begin();
  motor1->setSpeed(vel1);
 
  motor2->setSpeed(vel2);
 
  motor1->run(RELEASE);
  motor2->run(RELEASE);
 
  pinMode(RED,OUTPUT);//LED VERMELHO   
  pinMode(GREEN,OUTPUT);//LED GREENN   
  pinMode(BLUE,OUTPUT);//LED BLUE   //Inicia a comunicação serial
  pinMode (enc, INPUT); 
 
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, LOW);
  digitalWrite(RED, LOW);
 
  myPID.SetMode(AUTOMATIC);
  myPID2.SetMode(AUTOMATIC);
 
 
 
}
 
 
void printaOptico(){
 
while(1){
int estadoDir = analogRead(optDir);  //Lê o valor fornecido pelo LDR 
int estadoEsq = analogRead(optEsq); 
Serial.print(estadoDir);
Serial.print("  ");
Serial.println( estadoEsq);
} 
}
 
void printaLDR(){
 
while(1){
int LDR1 = analogRead(LDR);
int LDR2 = analogRead(LDR2);
Serial.print(LDR1);
Serial.print("  ");
Serial.print(LDR2);
Serial.print("  ");
Serial.println(LDR1-LDR2);
delay(500);
}
 
}
 
void loop() {
  int botao;  
  botao = analogRead (0);  //Leitura do valor da porta analógica A0
  lcd.setCursor(0,0);  
  lcd.print("TP4 Charlinho"); 
 
  if (botao < 100) {  
    // ---------------------- BOTAO RIGHT -----------------
 
    lcd.setCursor(0,0);  
    lcd.print("                           ");
    lcd.setCursor(0,0);  
    lcd.print("Teste Cor");
 
    checaCores();
    mostraCores();
 
 
  }  
  else if (botao < 200) {
    // ---------------------- BOTAO UP -----------------
 
    long pmi=millis();
    long pmf=0;
    lcd.setCursor(0,0);  
    lcd.print("                           ");
    lcd.setCursor(0,0);  
    lcd.print("MAX LUZ POLAR");
    while (pmf-pmi<5000){
      pmf=millis();
      pegamax();
    }
    motor1->run(RELEASE);
    motor2->run(RELEASE);
 
  }  
  else if (botao < 400){ 
    // -------------------- BOTAO DOWN --------------  
 
    lcd.setCursor(0,0);  
    lcd.print("                                   ");
    lcd.setCursor(0,0); 
    lcd.print("Pronto p/ Combate");
    espera();
    t_atual = millis();
    t_start = millis();
    ldr();
    gira_esq(180);
    frente(0.3);
    segLinha();
    motor1->run(RELEASE);
    motor2->run(RELEASE);
 
 
  }
  else if (botao < 600){ 
    // -------------------- BOTAO LEFT -------------- 
   frente(0.5);
   gira_esq(180);
   frente(0.5);
 
 
  }
  else if (botao < 800){  
    // BOTAO SELECT
 
    lcd.setCursor(0,0);  
    lcd.print("                   ");
    lcd.setCursor(0,0); 
    lcd.print("PRETO E BRANCO");
    checaBalanco();
 
 
    }
 
  }  
cursos/introrobotica/2019-1/grupo04/tp4.c.txt · Última modificação: por 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki