Hi, I'm helping my son build a useless machine from here:
I have downloaded the code from the link at the bottom of step 8:
http://www.instructables.com/files/orig/FQ2/DXL0/HMMF7678/FQ2DXL0HMMF7678.txt
and ran the verification and it worked. I then inserted some code for debouncing that was suggested by the author and I get:
'switchoff' was not declared in this scope
Down at the else if statements on (selectedMove == 7). Line 118 I believe.
I'm sure I'm doing something wrong when inserting the debounce code but I can't get why this would cause this error to pop up and at this location. The switchoff function is called twice before that on lines 111 and 112 without a problem.
Can anyone help me here?
Thanks
Marc
I had to shorten the code for word count but the full code is here:
https://drive.google.com/file/d/0B2RIY_75mEYxaTRpV1R1N055SFk/view?usp=sharing
Here is my edited code with the debounce inserted:
#include <Servo.h>
Servo doorServo;
Servo handServo;
// int switch_pin = 2; //set switch on pin 2
// ****************************************
// ****************************************
// Added 04-05-2016 by Marc from Riachi's software debounce sulition
const int switch_pin= 2; // the number of the pushbutton pin
int switch_pinState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
// End of this part of add by Marc 04-05-2016
// *****************************************
// *****************************************
//motor variables
int motorThrottle=11;
int motorDirection = 13;
int motorBrake=8;
//Distance Variables
int motionPin = 0; //motion sensor pin on analog 0
int lastDist = 0; //to remember last distance
int currentDist = 0;
int thresh = 100; //Threshold for Movement
int pos = 0;
int selectedMove = 0; //move selector
int Testmove = 0; //test mode: set to move number to test only one selected move
//(set to Zero to run normally i.e: roundrobbin on amm moves)
void setup()
{
Serial.begin(9600);
pinMode(switch_pin, INPUT);
doorServo.attach(9); //set door servo on Pin 9 pwm
handServo.attach(10); //set hand servo on Pin 10 pwm
doorServo.write(80); //set door to hiding position
handServo.write(0); //set hand to hiding position
//Setup Channel B, since Channel A is reserved by door and hand servos and can't ber used at same time
pinMode(motorDirection, OUTPUT); //Initiates Motor Channel B pin
pinMode(motorBrake, OUTPUT); //Initiates Brake Channel B pin
}
void loop()
{
if (Testmove != 0) {
selectedMove = Testmove;
}
// **************************
// **************************
// Statement taken out by Marc 04-05-2016 to insert debounce code
//if the switch is on, then move door and hand to switch it off...
// if(digitalRead(switch_pin) == HIGH)
// End of takeout statement by marc 04-05-2016
// **************************
// *************************
// *************************
//**************************
// insetr statement by Marc for debounce 04-05-2016 - Code from Riachi
// read the state of the switch into a local variable:
int reading = digitalRead(switch_pin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != switch_pinState) {
switch_pinState = reading;
// only activate the hand and door servo if the switch button state is HIGH
if (switch_pinState == HIGH) {
//do all the machine's code here because you've detected that the user has actually switched the box ON, it wasn't noise
// end of insert statement by Marc for debounce 04-05-2016
// *********************************
// *********************************
{
if (selectedMove > 10) { selectedMove = 0; } //when all moves are played, repeat the moves from beginning
if (selectedMove == 0) { switchoff(); }
else if (selectedMove == 1) { switchoff(); }
else if (selectedMove == 2) { switchoffbitaraddod(); }
else if (selectedMove == 3) { crazydoor(); }
else if (selectedMove == 4) { crazyslow(); }
else if (selectedMove == 5) { m7anika7anika(); }
else if (selectedMove == 6) { m3alla2(); }
else if (selectedMove == 7) { switchoff(); }
else if (selectedMove == 8) { matrix(); }
else if (selectedMove == 9) { sneak(); }
else if (selectedMove == 10) { zee7(); }
if (Testmove == 0) {
selectedMove++; //swith to next move if not in test mode
}
}
}