Push Button As a Switch

I really need some help.
I want to use an Octopus Digital Push Button with a LED light as a switch.
Push button set as the LED is off.
Basic concept is that when I push the button one time, the LED turns on.
And then, when I push the button again, the LED turns off and other systems codes, eye tracking and laser sensor, start.
When I push again, LED turns on again and other systems stop.

My code has some problems.
The LED start at off.
When I push the button one time, the LED does not turn on.
Also, eye tracking code works at the time, but laser sensor does not work.
When I push the button again, the LED keep staying off and eye tracking and laser sensor does not turn off.

Please, help me to figure it out.

#include <Stepper.h>
#include <SPI.h>  
#include <Pixy.h> 
#include<stdio.h>
using namespace std;

Pixy pixy;

Stepper motor(200,12,13);
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
// variable to store the stepper position 

float y_scale = 0.5; //control step scale
int previous = 0;

// Laser sensor and receiver
int Laser = 6; // # of pin of Laser
int Detector = 7; // # of pin of Detector

// Push button
int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous1 = HIGH;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers


void setup() 
{ 
  Serial.begin(19200);
  Serial.print("Starting...\n");

  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  pixy.init();
  motor.setSpeed(80);
  
//Laser and Receiver
  pinMode(Laser, OUTPUT);
  pinMode(Detector, INPUT);

// Push Button
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
} 

void loop() 
{ 

  
//------------------
// Eye Tracking
//------------------
  reading = digitalRead(inPin);

if (reading == HIGH && previous1 == LOW && millis() - time > debounce)
  {
    if (state == LOW)
    {
      state = LOW;
      Serial.print("OFF");

      //-----------------
      // L aser sensor
      //-----------------
      digitalWrite(Laser, HIGH);
      boolean val = digitalRead(Detector);
      delay (500);
      Serial.print(val); // 1 is detector, 0 is blocked


// eye tracking start;

      static int i = 0;
      int j;
      uint16_t blocks;
      char buf[36];
      blocks = pixy.getBlocks();
 
      int x,y,y1,y2,y3,y4,ypos1,ypos2,ypos3,ypos4,Detector;
      int step1 = 100; //center position of pixy

      if(val=1) // checking that laser is not at center
        {
          if (blocks)
            {
              i++;
              for (j=0; j<blocks; j++)
                {
                  if (pixy.blocks[j].height > 1)
                    {
                      if (pixy.blocks[j].y > 10 && pixy.blocks[j].y < 100)
                       {
                        y1 = pixy.blocks[j].y;
                        motor.step(y_scale * (previous - y1));
                        previous = y1;
                       }
                       else if (pixy.blocks[j].y > 100 && pixy.blocks[j].y < 180)
                        {
                          y1 = pixy.blocks[j].y;
                          motor.step(y_scale * (previous - y1));
                          previous = y1;
                        }
                    }
                } // end of motor movement code
            }
        }
            else if(val=0) // if the laser is at center
            {
            y1=0;y2=0;y3=0;y4=0;ypos1=0;ypos2=0;ypos3=0;ypos4=0; x=0;
            }
    }
  else if(reading == HIGH && previous == LOW && millis() - time > debounce)
    {
      state = HIGH;
      Serial.print("ON");
    }
    
 time = millis();    
  }
  digitalWrite(outPin, state);
  previous1 = reading;
  }

if(val=1) // checking that laser is not at center

if(val==1) // checking that laser is not at center

Thank you for finding the error. However, the button does not still work like the concept. I do not know what problem is on the code.

using namespace std;

Useless. Get rid of this. You should NEVER include the entire std namespace. At most, you should have: using std::xxxx; whenever you use xxxx from the std namespace.

  pinMode(inPin, INPUT);

This tells us NOTHING about how the switch is wired. Inevitably, when someone fails to use the internal pullup resistor, and has trouble with a switch, it is because the switch is not wired correctly. Post a diagram showing how your switch is wired.

Or, use the INPUT_PULLUP mode, and connect one leg of the switch to ground and the other leg to the digital pin. HIGH will mean no pressed; LOW will mean pressed.

I attached the diagram which explain how I connect Arduion with other stuff.
The equipment in the picture is not same as what I use, but wire connection are same.
The push button has three pins: signal, voltage, GND.
So, pinMode(inPin, INPUT) means the signal of push button.
I changed the INPUT to INPUT_PULLUP, but it does not still work.
I think the loop has some problems, not wire.

#include <Stepper.h>
#include <SPI.h>  
#include <Pixy.h> 
#include<stdio.h>

Pixy pixy;

Stepper motor(200,12,13);
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
// variable to store the stepper position 

float y_scale = 0.5; //control step scale
int previous = 0;

// Laser sensor and receiver
int Laser = 6; // # of pin of Laser
int Detector = 7; // # of pin of Detector

int ledPin = 13;
int buttonPin = 2;

boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
boolean ledState = LOW;//storage for the current state of the LED (off/on)

void setup(){
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  pixy.init();
  motor.setSpeed(80);
  
//Laser and Receiver
  pinMode(Laser, OUTPUT);
  pinMode(Detector, INPUT);

  pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);//initialize Serial connection
}

void loop(){
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW){//if button has just been pressed
    Serial.println("pressed");
    delay(1);//crude form of button debouncing
    
    //toggle the state of the LED
    if (ledState == HIGH){
      digitalWrite(ledPin, LOW);
      ledState = LOW;

      //-----------------
      // L aser sensor
      //-----------------
      digitalWrite(Laser, HIGH);
      boolean val = digitalRead(Detector);
      delay (500);
      Serial.print(val); // 1 is detector, 0 is blocked


// eye tracking start;

      static int i = 0;
      int j;
      uint16_t blocks;
      char buf[36];
      blocks = pixy.getBlocks();
 
      int x,y,y1,y2,y3,y4,ypos1,ypos2,ypos3,ypos4,Detector;
      int step1 = 100; //center position of pixy

      if(val==1) // checking that laser is not at center
        {
          if (blocks)
            {
              i++;
              for (j=0; j<blocks; j++)
                {
                  if (pixy.blocks[j].height > 1)
                    {
                      if (pixy.blocks[j].y > 10 && pixy.blocks[j].y < 100)
                       {
                        y1 = pixy.blocks[j].y;
                        motor.step(y_scale * (previous - y1));
                        previous = y1;
                       }
                       else if (pixy.blocks[j].y > 100 && pixy.blocks[j].y < 180)
                        {
                          y1 = pixy.blocks[j].y;
                          motor.step(y_scale * (previous - y1));
                          previous = y1;
                        }
                    }
                } // end of motor movement code
            }
        }
            else if(val==0) // if the laser is at center
            {
            y1=0;y2=0;y3=0;y4=0;ypos1=0;ypos2=0;ypos3=0;ypos4=0; x=0;
            }      
    } else {
      digitalWrite(ledPin, HIGH);
      ledState = HIGH;
    }
  }
 lastState = currentState;

}

I used another push button code with eye tracking code.
The code made the LED light turn on and off.
However, the eye tracking code worked at the moment when I push the button.
My idea is using For Loop instead of If Loop.
I am not sure about that.
If you have some ideas, please tell me specifically.

Project.png

I changed the INPUT to INPUT_PULLUP, but it does not still work.

I don't see that.

You need to write a sketch that does NOTHING but read the switch state and write to the serial monitor. Start with the state change detection example. When the switch changes state, print "Switch is pressed" or "Switch is released".

With the internal pullup enabled, one leg of the switch is connected to the digital pin. The other leg is connected to ground.

I have no idea why your switch has three wires.

LED toggle code.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

byte oldPinState = HIGH;

void setup ()
  {
  pinMode (buttonPin, INPUT_PULLUP);  
  pinMode (ledPin, OUTPUT);
  }  // end of setup

void loop ()
  {
  byte pinState = digitalRead (buttonPin);
  if (pinState != oldPinState)
    {
    delay (10);  // debounce 
    if (pinState == LOW)
      digitalWrite (ledPin, !digitalRead (ledPin));  // toggle pin
    oldPinState = pinState;  // remember new state
    }  // end of if pin state change
  }  // end of loop

I used first toggle code.
However, when I put my eye tracking code after toggle = !toggle like below code, eye tracking code does not work.
When I push the button, eye tracking code works just at the moment and stop.

else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;

// eye tracking code start;

      static int i = 0;
      int j;
      uint16_t blocks;
      char buf[36];
      blocks = pixy.getBlocks();
 
      int x,y,y1,y2,y3,y4,ypos1,ypos2,ypos3,ypos4,Detector;
      int step1 = 100; //center position of pixy

      if(val==1) // checking that laser is not at center
        {
          if (blocks)
            {
              i++;
              for (j=0; j<blocks; j++)
                {
                  if (pixy.blocks[j].height > 1)
                    {
                      if (pixy.blocks[j].y > 10 && pixy.blocks[j].y < 100)
                       {
                        y1 = pixy.blocks[j].y;
                        motor.step(y_scale * (previous - y1));
                        previous = y1;
                       }
                       else if (pixy.blocks[j].y > 100 && pixy.blocks[j].y < 180)
                        {
                          y1 = pixy.blocks[j].y;
                          motor.step(y_scale * (previous - y1));
                          previous = y1;
                        }
                    }
                } // end of motor movement code
            }
        }
            else if(val==0) // if the laser is at center
            {
              motor.step(y_scale*(step1-previous));
              //y1=0;y2=0;y3=0;y4=0;ypos1=0;ypos2=0;ypos3=0;ypos4=0; x=0;
            }
    }