Author Options:

Help with Arduino code. Works fine, then ignores parts of the code. Answered

Alright, a bit of a complicated question, so bear with me (if you dare)

__Purpose of project:__
Build a robot that solves the game "Fastest King" as fast as possible


Here is a video of the game being played. The object is to clear the descending squares as fast as humanly (or robotically, in this case) possible.

__Method of attack:__
-Four servos with attachments that allow interface with capacitve touch screens are positioned above each button
-Four Photoresistors detect when a square is in the bottom position, ready to be pressed.
-Potentiometer to control the speed of the arms and how fast they return to the "unpressed" position (denoted in the code by "d" or the delay in between the servo write commands)
-On off switch, of course
-All I/O is controlled by an arduino nano 328

apologies for the terrible image quality, can update with better ones if necessary.

(seems simple enough, right?)

__Description of problem:__
Everything works swimmingly, except the game doesn't "remove" the square as soon as the correct button is pressed. this causes, when "d" is sufficiently low (100-75ms), the servos to press more than once for each square, causing mistakes within the game, slowing the overall time down.

__Attempted fix:__
I attempted to implement some code that would cause each if statement checking for the square to also check that it hadn't pressed earlier within a certain time "doubletapdelay" effectively adding a delay in between doubletaps but leaving the rest of the servos free to activate.
the odd thing is, the code i put in, (visible below, relevant parts bolded and commented) works well, but sometimes is completely ignored. even with the doubletap delay at 1000 ms while testing it, it would occasionally run as if it wasn't checking for double taps at all.

If you think you can help out, or have a better idea for a way to do anything in this project, feel free to let me know.
also, i can post any information you would like about this such as additional pictures or video, (though video is a bit difficult since my phone is inside of the machine whilst operating.

Thanks again guys,
you  rock.

important parts bolded

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

boolean activate;

int x;
int i;
int d = 125;
int e;

int inputYellow = 0;
int inputBlue = 1;
int inputGreen = 2;
int inputPink = 3;
int potPin1 = 4;
int toggle = 12;

int input1;
int input2;
int input3;
int input4;

int threshold = 450;

int thresholdYellow = 200;
int thresholdBlue = 400;
int thresholdGreen = 200;
int thresholdPink = 320;

int doubletapdelay = 125;

int delayYellow = 0;
int delayBlue = 0;
int delayGreen = 0;
int delayPink = 0;

void setup() {


void loop() {

d = map(analogRead(potPin1), 0, 1023, 50, 200);

if(digitalRead(toggle) == HIGH) {
   activate = false;
} else { activate = true;

  if(activate) {

if(analogRead(inputYellow) < thresholdYellow ){    // checks light level on  photoresistor
  if((millis() - delayYellow) >= doubletapdelay){    // checks to see when last time this servo was activated
  Serial.print("Yellow: "); Serial.println(analogRead(inputYellow));   //prints if servo activates successfully
    delayYellow = millis();   
//records last time servo successfully activated
    Serial.println("yellow locked out"); 
   //prints if servo tried again too soon

if(analogRead(inputBlue) < thresholdBlue ){
  if((millis() - delayBlue) >= doubletapdelay){
  Serial.print("Blue: "); Serial.println(analogRead(inputBlue));
    delayBlue = millis();
    Serial.println("blue locked out");

if(analogRead(inputGreen) < thresholdGreen ){
  if((millis() - delayGreen) >= doubletapdelay){
  Serial.print("Green: "); Serial.println(analogRead(inputGreen));
    delayGreen = millis();
    Serial.println("green locked out");
if(analogRead(inputPink) < thresholdPink ){
  if((millis() - delayPink) >= doubletapdelay){
  Serial.print("Pink: "); Serial.println(analogRead(inputPink));
    delayPink = millis();
    Serial.println("pink locked out");
} else {



Change the code to look, if an arm is down, for when the light isn't present.

Don't overcomplicated things with double taps. Slow the program down with some short delays after the servo is activated. Giving the game time to remove the square before your code moves on to check for squares again. It's all about fine tuning the code to work with the delay in the game.

that is the general idea, and i can get it to work with no errors by dialing down the speed with the attached potentiometer (not pictured in the images), however, the object of the game is speed, and avoiding errant double taps seems to be the only way to prevent errors at high speeds.

Either way you cut it the code is moving faster then the game is able to run. Whether you have the code run some added debounce code or add a delay your still just slowing the code down so it's not outpacing the game.

But if you want to learn to use debounce code then check out the Arduino Debounce example code.

Do you mean debounce the input from the photoresistor. I never thought of that. Thats a great idea. Thanks.