loading

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

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

http://www.youtube.com/watch?v=GwGBUKCoIBs

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

IMAGES
http://imgur.com/a/EprMu
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.



-------------------------------------------------code---------------------------------------------------------
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() {
Serial.begin(9600);
servo1.attach(9);
servo2.attach(8);
servo3.attach(7);
servo4.attach(6);

pinMode(toggle,INPUT_PULLUP);
servo1.write(180);
servo2.write(90);
servo3.write(110);
servo4.write(90);
delay(1000);
}


void loop() {


d = map(analogRead(potPin1), 0, 1023, 50, 200);
if(!activate)Serial.println(d);

 
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
    servo1.write(120);
    delay(d);
    servo1.write(130);
    delay(d);
    delayYellow = millis();   
//records last time servo successfully activated
   
    }else{
    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));
    servo2.write(17);
    delay(d);
    servo2.write(30);
    delay(d);
    delayBlue = millis();
    }else{
    Serial.println("blue locked out");
  }
}

if(analogRead(inputGreen) < thresholdGreen ){
  if((millis() - delayGreen) >= doubletapdelay){
  Serial.print("Green: "); Serial.println(analogRead(inputGreen));
    servo3.write(15);
    delay(d);
    servo3.write(40);
    delay(d);
    delayGreen = millis();
    }else{
    Serial.println("green locked out");
  }
}   
   
if(analogRead(inputPink) < thresholdPink ){
  if((millis() - delayPink) >= doubletapdelay){
  Serial.print("Pink: "); Serial.println(analogRead(inputPink));
    servo4.write(160);
    delay(d);
    servo4.write(140);
    delay(d);
    delayPink = millis();
    }else{
    Serial.println("pink locked out");
  }
}   
} else {
servo1.write(180);
servo2.write(90);
servo3.write(110);
servo4.write(90);
}



}

Picture of Help with Arduino code. Works fine, then ignores parts of the code.
20130619_170046.jpg
20130619_183746.jpg
sort by: active | newest | oldest
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.
awesomesquared (author)  mpilchfamily4 years ago
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.
+1
awesomesquared (author)  mpilchfamily4 years ago
Do you mean debounce the input from the photoresistor. I never thought of that. Thats a great idea. Thanks.