/*
Autor: David Lucas
Eng. inst. Faro (Faculdade de Roseira)
Titulo: Braço Robótico com sensor LDR
*/
#include <Servo.h>
const int Botao1 = 2;
const int Botao2 = 3;
int EstadoBotao1 = 0;
int EstadoBotao2 = 0;
Servo motor1;
Servo motor2;
Servo motor3;
Servo motor4;
int ldr0 = 0;
int A = 0;
int ldr1 = 1;
int B = 1;
int ldr2 = 2;
int C = 2;
int ldr3 = 3;
int D = 3;
int a0 = 90;
int b1 = 0;
int c2 = 0;
int d3 = 0;
int n=1;
int nrotation=10; //n de rotação por pressionar o botão
void setup() {
Serial.begin(9600);
motor4.attach(8);
pinMode(Botao1, INPUT);
pinMode(Botao2, INPUT);
}
void loop() {
A = analogRead (ldr0);
B = analogRead (ldr1);
C = analogRead (ldr2);
D = analogRead (ldr3);
Serial.print("D");
Serial.println(d3);
/*comando a cima somente serial monitor */
/*motor 1*/
if ( A < 100){
delay (100);
motor1.attach(5);
EstadoBotao1 = digitalRead (Botao1);
EstadoBotao2 = digitalRead (Botao2);
if( EstadoBotao1 == HIGH ){
Serial.print ("BT + Ligado");
a0= a0-nrotation;
motor1.write(a0);
}
else{
if( EstadoBotao2 == HIGH ){
Serial.print ("BT - Ligado");
a0= a0+nrotation;
motor1.write(a0);
}
}
}
else{
/*motor 2*/
if ( B < 100){
motor2.attach(6);
EstadoBotao1 = digitalRead (Botao1);
EstadoBotao2 = digitalRead (Botao2);
if( EstadoBotao1 == HIGH ){
Serial.print ("BT + Ligado");
b1= b1-n;
motor2.write(b1);
}
else {
if( EstadoBotao2 == HIGH ){
Serial.print ("BT - Ligado");
b1 = b1 +n;
motor2.write(b1);
}
}
}
else {
/*motor 3*/
if ( C < 100){
motor3.attach(7);
EstadoBotao1 = digitalRead (Botao1);
EstadoBotao2 = digitalRead (Botao2);
if( EstadoBotao1 == HIGH ){
Serial.print ("BT + Ligado");
c2= c2-n;
motor3.write(c2);
}
else {
if( EstadoBotao2 == HIGH ){
Serial.print ("BT - Ligado");
c2 = c2 +n;
motor3.write(c2);
}
}
}
else{
/*motor 4*/
if ( D < 100){
motor4.attach(8);
EstadoBotao1 = digitalRead (Botao1);
EstadoBotao2 = digitalRead (Botao2);
if( EstadoBotao1 == HIGH ){
Serial.print ("BT + Ligado");
d3= d3-n;
motor4.write(d3);
}
else {
if( EstadoBotao2 == HIGH ){
Serial.print ("BT - Ligado");
d3 = d3 +n;
motor4.write(d3);
}
}
}
}
}
}
}