arduino_hack_day / turiquinhas.lde

/*
 * Copyright (C) 2010 Álvaro Justen <alvaro@justen.eng.br>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 dated June, 1991.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * If you find any bugs or have any suggestions, please email me.
 *
 * This software was created on Arduino Hack Day Reloaded (2010-04-01) 
 * See more details and authors at:
 * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-day-reloaded.html
 * 
 */

#define FREQ_ANTI 250
#define FREQ 128

int sensorValorEsq;
int sensorValorDir;
int sensorValorFrente;
int sensorValorTras;
int potenciometro;
#ifdef DEBUG
char enviar[10];
#endif

const int PinRodaDir = 6;
const int PinRodaEsq = 7;
const int pinPotenciometro = 10;
const int ledPinDir = 8;
const int ledPinEsq = 9;
const int ledPinStopped = 10;
const int buzzer = 11;
const int analogInDir = 6;
const int analogInEsq = 7;
const int analogInFrente = 8;
const int analogInTras = 9;

void startEsquerda() {
  analogWrite(PinRodaEsq, FREQ_ANTI);
}

void startDireita() {
  analogWrite(PinRodaDir, FREQ);
}

void trasDireita() {
  analogWrite(PinRodaDir, FREQ_ANTI);
}
void trasEsquerda() {
  analogWrite(PinRodaEsq, FREQ);
}

void stopDireita() {
    analogWrite(PinRodaDir, 0);
}

void stopEsquerda() {
    analogWrite(PinRodaEsq, 0);
}

void setup() {
  pinMode(PinRodaEsq, OUTPUT);  
  pinMode(PinRodaDir,OUTPUT);
  pinMode(ledPinDir,OUTPUT);
  pinMode(ledPinEsq,OUTPUT);
  pinMode(ledPinStopped,OUTPUT);
  analogWrite(buzzer, 0);

  stopDireita();
  stopEsquerda();
  #ifdef DEBUG
  Serial.begin(9600);
  #endif
}

void loop() {
  potenciometro = analogRead(pinPotenciometro);
  
  sensorValorEsq = analogRead(analogInEsq);
  sensorValorDir = analogRead(analogInDir);
  sensorValorFrente = analogRead(analogInFrente);
  sensorValorTras = analogRead(analogInTras);
  
  int diff = sensorValorEsq - sensorValorDir;
  
  if (sensorValorFrente >= potenciometro) {
    startDireita();
    startEsquerda();
    analogWrite(buzzer, 0);
    digitalWrite(ledPinStopped, HIGH);
    digitalWrite(ledPinEsq, LOW);
    digitalWrite(ledPinDir, LOW);
  }
  else if (sensorValorTras >= potenciometro) {
    trasDireita();
    trasEsquerda();
    analogWrite(buzzer, 128);
    digitalWrite(ledPinStopped, LOW);
    digitalWrite(ledPinEsq, LOW);
    digitalWrite(ledPinDir, LOW);
  }
  else if (sensorValorEsq >= potenciometro) {
    startEsquerda();
    stopDireita();
    analogWrite(buzzer, 0);
    digitalWrite(ledPinStopped, HIGH);
    digitalWrite(ledPinEsq, HIGH);
    digitalWrite(ledPinDir, LOW);
  }
  else if (sensorValorDir >= potenciometro) {
    stopEsquerda();
    startDireita();
    analogWrite(buzzer, 0);
    digitalWrite(ledPinStopped, HIGH);
    digitalWrite(ledPinEsq, LOW);
    digitalWrite(ledPinDir, HIGH);
  }
  else {
    stopDireita();
    stopEsquerda();
    analogWrite(buzzer, 0);
    digitalWrite(ledPinStopped, HIGH);
    digitalWrite(ledPinEsq, LOW);
    digitalWrite(ledPinDir, LOW);
  }

  #ifdef DEBUG
  sprintf(enviar, "%04d %04d", sensorValorEsq, sensorValorDir);
  Serial.println(enviar);
  #endif
  
  //delay(100);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.