Hi. I was trying to combine 2 sketches together - Remote & Wall Avoidance. It kinda works. Remote always works (guess because it’s in Loop), but Wall Avoidance only runs once on press of the “-” button (it’s outside Loop function. Any ideas how to fix this please?
Here’s the code:
//Based on 2 seperate sketches by Arcbotics (Remote & Wall Avoidance).
//Trying to combine both to get Wall Avoidance to switch on with remote button "-".
//Issues - Remote works fine.
//Wall Avoidance turns on but only runs once. Only checks distance of 10cm at start.
/*******************************************
IR Remote
Sparki has a sensor that lets it receives
commands from the included remote control.
Try moving it around!
********************************************/
#include <Sparki.h> // include the sparki library
void setup()
{
sparki.clearLCD();
}
// /------^-----\
// | |
// | 69 70 71 |
// | 68 64 67 |
// | 7 21 9 |
// | 22 25 13 |
// | 12 24 94 |
// | 8 28 90 |
// | 66 82 74 |
// \____________/
void loop()
{
int code = sparki.readIR();
if(code != -1){
sparki.print("Received code: ");
sparki.println(code);
}
switch(code){
// Movement buttons
case 70: sparki.moveForward(); break;
case 21: sparki.moveBackward(); break;
case 67:
case 71: sparki.moveRight(); break;
case 69:
case 68: sparki.moveLeft(); break;
case 64: sparki.moveStop();
sparki.gripperStop();
break;
// Gripper Buttons
case 9: sparki.gripperOpen(); break;
case 7: sparki.gripperClose(); break;
// buzzer
case 13: sparki.beep(); break;
// Servo Buttons
case 12: sparki.servo(SERVO_LEFT); break;
case 24: sparki.servo(SERVO_CENTER); break;
case 94: sparki.servo(SERVO_RIGHT); break;
// RGB LED
case 90: sparki.RGB(RGB_OFF); break;
case 66: sparki.RGB(RGB_RED); break;
case 82: sparki.RGB(RGB_GREEN); break;
case 74: sparki.RGB(RGB_BLUE); break;
case 22: wallAvoidance(); break;
default:
break;
}
sparki.updateLCD();
}
void wallAvoidance()
{
//Wall Avoidance
sparki.servo(SERVO_CENTER);
sparki.moveForward(); // move forward
sparki.RGB(RGB_GREEN); // turn the light green
int repeat = 0; // TREVOR - was trying this to get this function to repeat indefinately, but not working
int cm = sparki.ping(); // measures the distance with Sparki's eyes
if(cm != -1) // make sure its not too close or too far
{
if(cm < 20) // if the distance measured is less than 10 centimeters
{
sparki.RGB(RGB_RED); // turn the light red
sparki.beep(); // beep!
sparki.moveBackward(10); // back up 10 centimeters
sparki.moveRight(80); // rotate right 30 degrees
delay(1000);
}
}
delay(100); // wait 0.1 seconds (100 milliseconds)
}

