Hello all!
I´m a newbie on arduino, just a few basic ideas .
Now, I´m trying to build a carriage to go away and came back on hanging on a wire, I use a RC ESC to control motor speed regulated via potentiometer and i´m trying to implemet a motor turn impluse detector to know what distance carriage is from starting point.
The problem that is killing me is that a get a initial phototransistor reading, for compare, and then if I move potentiometer phototransistor readings change. Now, what do you think can cause this? Bad electonics, bad programing.
I have the code below now but already made many many changes and variations.
Many thanks in advance.
#include <Servo.h>
#include <LiquidCrystal.h>
Servo myservo; // create servo
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// ---------- pins IN OUT ----------------
//int analogPin= 5; //fototransistor analog pin
int relePin = 6; // pin relay
int tempPin = 1; // pin analogico
int potPin = 2; // analog pin potenciometro
int ledPin = 13; // pin LED
int pulsoPin = 5; //detector de impulsos
int ledredPin = 7;
int ledgreenPin = 10;
// --------------- variaveis ----------------
int val; // valor do potenciometro
int valservo; // valor servo (BEC)
int temp; //temperatura
int sentido; //sentido do alvo
int pulso; // estado dos impulsos 1 ou 0
int fotoref; // valor inicial fototransistor
int valfoto; // valor do fototransistor
float distinicial = 3.50; // distancia inicial
float distpulso = 0.12; //distancia percorrida a cada impulso
float distancia = 0;
void setup() {
lcd.begin(20, 2);
Serial.begin(9600);
myservo.write(0); // manda parar servo no arranque
pinMode(pulsoPin, INPUT);
pinMode(relePin, OUTPUT);
pinMode(ledredPin, OUTPUT);
pinMode(ledgreenPin, OUTPUT);
myservo.attach(9);
fotoref = analogRead(pulsoPin);
pinMode(tempPin, INPUT);
distancia = distinicial;
lcd.clear();
lcd.print("ALVO A");
lcd.setCursor(14,0);
lcd.print("Metros");
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.setCursor (5,1);
temp = analogRead(tempPin);
temp = (5.0 * temp * 100.0)/1024.0;
lcd.setCursor (5,1);
lcd.print(temp);
//temp = temp * 0.48828125;
}
void loop() {
lcd.setCursor(8,0);
lcd.print(distancia);
// ------------ detector de impulsos ------------
valfoto = analogRead(pulsoPin);
Serial.print(fotoref);
Serial.print(" foto ");
Serial.println(valfoto);
if (valfoto >= fotoref) {
pulso= 1;
}
else
{
pulso=0;
}
delay(200);
// ------------- movimento -------------
val = analogRead(potPin);
if (val > 590 || val > 640);
{
myservo.write(0);
digitalWrite(ledgreenPin, LOW);
digitalWrite(ledredPin, LOW);
}
// ------------- movimento motor avanço ---------
if (val <= 590 && pulso == 1 ){
valservo = map(val,0 ,590 ,95 ,50);
digitalWrite(relePin, LOW);
digitalWrite(ledgreenPin, HIGH);
digitalWrite(ledredPin, LOW);
myservo.write(valservo);
delay(10);
//Serial.println(valservo);
if (valfoto < fotoref-15){
distancia = distancia - distpulso;
}
//lcd.setCursor(7,0);
//lcd.print(distancia);
}
//else
//-------------movimento do motor recuo ----------
if (val >=640 && pulso == 1 ){
valservo=map(val,640 ,1023 ,50 ,95);
digitalWrite(relePin, HIGH);
digitalWrite(ledgreenPin, LOW);
digitalWrite(ledredPin, HIGH);
myservo.write(valservo);
delay(10);
//Serial.println(valservo);
if (valfoto < fotoref-15){
distancia = distancia + distpulso;
}
//lcd.setCursor(7,0);
//lcd.print(distancia);
}
/*if (val > 590 || val > 640);
{
myservo.write(0);
digitalWrite(ledgreenPin, LOW);
digitalWrite(ledredPin, LOW);
}*/
}