Problem with potentiometer changing phototransistor readings HELP

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);
  }*/

}

Here I drawled you up a the idea way to hook up a CDS photo-resistor

Now the photo-transistor is a "on" or off switch so are you using a potentiometer
to set the beam output like the drawing showing how to use a photo-transistor

logic.PNG

setCDS.PNG

Thank you for your quick answer.

My pot is connected like this:

(+ )----->|---(A2)---|<----- (-)

I´ll modify it and add the resistors.

PS. another thing is to connect phototransistor to a digital pin I belive!?

You can read it as a switch which is the best way to use them. The output is hooked up to a digital pin.

On the logic.png you have a resistor symbol inside a circle... what does it mean? probably is a silly question...

Many thanks for the help :slight_smile:

I drawled how to use both a photo- transistor top drawing.

And a photo- resistor bottom drawing

The photo-resistor let's you read the light by voltage could be used to slow the motor down as you get close to your stopping point.

Where as the top picture the photo-transistor is more for a on and off state.

I´ve tried the logic.png but no success, changed other things and I cant see state change from HIGH to LOW ever.

at the moment just need a project to detect pulses from a phototransistor capable of made something happend and that remain stable when we turn potentiometer.

Many thanks.

this is driving me crazy .... :frowning:

If your outside in the day light a photo-transistor not going to work very good.