not executing entire sketch

Hello,

I am somewhat new to programming and am adapting someone else’s code. The trouble that I am having is that not all parts of the sketch are executed. Here is the code I am using (it’s not complete yet):

/*
 *Autonomous Robot
 */

#include <Servo.h>

//wheel servo
#define RSTOP 90
#define LSTOP 90
#define RFORWARD 0
#define RREVERSE 180
#define LFORWARD 180
#define LREVERSE 0

//head servo
#define CENTER 90
#define RIGHT 0
#define LEFT 180
#define RCENTER 45
#define LCENTER 125

//convinience
#define ERR 50
#define LEFTTURN 0
#define DEBUG 0

Servo leftwheel;
Servo rightwheel;
Servo head;

int leftspeeed = LFORWARD;
int rightspeed = RFORWARD;
int headposition = CENTER;

int leftdist = 0;
int rightdist = 0;
int frontdist = 0;
int eyes = 5;

int curdist = 0;
int objdist = 0;
int objdir = 0;


boolean lookingright = true;
boolean turnNow = false;


void setup(){
  leftwheel.attach (9);
  rightwheel.attach(10);
  head.attach(11);

  rightwheel.write(RSTOP);
  leftwheel.write(LSTOP);
  head.write(headposition);


  Serial.begin(9600);
}

/* go - full speed ahead
 * stop - dead stop
 * turn - turn in place for duration miliseconds
 */

void go(){
  rightwheel.write(RFORWARD);
  leftwheel.write(LFORWARD);
}

void stop(){
  rightwheel.write(RSTOP);
  leftwheel.write(LSTOP);
}

void turn(int dir){
  // store current speeds
  rightspeed = rightwheel.read();
  leftspeed = leftwheel.read();

  if(dir == LEFTTURN){
    rightwheel.write(RREVERSE);
    leftwheel.write(LFORWARD);
  }
  else {
    rightwheel.write(RFORWARD);
    leftwheel.write(LREVERSE);
  }
  delay(800);

  //restore original speeds
  rightwheel.write(rightspeed);
  leftwheel.write(leftspeed);
}

/* scan - turns head 1o and records distance and heading. 
 * It only saves closest object info. 
 * objdist and objdir hold the closest object data
 */

void scan(){

  int n = head.read();

  if(lookingright = true){
    head.write(n - 1);
  }
  if(n == 0) {
    lookingright = false;
    frontdist = 0;
    objdist = 0;
    turnNow = true;
  }

  if(lookingright = false){
    head.write(n + 1);
  }
  if(n == 180){
    lookingright = true;
    frontdist = 0;
    objdist = 0;
    turnNow = true;
  }

  Serial.println(n);

  curdist = analogRead(eyes);
  if(curdist > objdist){
    objdist = curdist;
    objdir = head.read();
    if(DEBUG){
      Serial.print("New close object at distane: ");
      Serial.print(objdist);
      Serial.print(" heading: ");
      Serial.println(objdir);
    }
  }
  delay(10);
}

void loop(){
  scan();

}

The problem lies where lookingright = true. The way I understand what I have written is that when lookingright is true, n which is the reading of the current servo position will decrease by 1. This value is then written to the servo until n reaches 0 where lookingright will then be set to false. The problem is that when n reaches 0 nothing happens, it only stays at 0. Also, the scan part of the sketch is not executed past the Serial.println(n) line.

Any help is appreciated.

 if(lookingright = true){
    head.write(n - 1);
  }

use == instead of = here to do a comparison, or you'll set lookingright to true every time. (same when comparing to false)

Not quite the same when assigning false, because an assignment of false will evaluate to false, so the conditional will never be executed, wheras a true assignment will always be true.

A good habit is to write

if(true == lookingright)

The comp will not allow you to drop one equal sign :) So, you can find the error during the compilation phase.

Even simpler is 'if (lookingright)'

double equal sign makes the sketch work perfectly. thanks a lot.