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;
}