Push Button As a Switch


i need help.
i want use octopus digital push button led light switch.
push button set led off.
basic concept when push button 1 time, led turns on.
and then, when push button again, led turns off , other systems codes, eye tracking , laser sensor, start.
when push again, led turns on again , other systems stop.

my code has problems.
the led start @ off.
when push button 1 time, led not turn on.
also, eye tracking code works @ time, laser sensor not work.
when push button again, led keep staying off , eye tracking , laser sensor not turn off.

please, me figure out.

code: [select]


#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 store stepper position

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

// laser sensor , receiver
int laser = 6; // # of pin of laser
int detector = 7; // # of pin of detector

// push button
int inpin = 2;         // number of input pin
int outpin = 13;       // number of output pin

int state = low;      // current state of output pin
int reading;           // current reading input pin
int previous1 = high;    // previous reading input pin

// follow variables long's because time, measured in miliseconds,
// become bigger number can stored in int.
long time = 0;         // last time output pin toggled
long debounce = 200;   // debounce time, increase if 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 , 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 detector, 0 blocked


// eye tracking start;

      static int = 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 laser not @ center
        {
          if (blocks)
            {
              i++;
              (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 laser @ 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 laser not @ center
 
 if(val==1) // checking laser not @ center
   


Arduino Forum > Using Arduino > Programming Questions > Push Button As a Switch


arduino

Comments