Code Help Please

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)


  
  }

Hi again. Any help on this please. I’m trying to a function for the wall avoidance from a button on the remote. Any idea how to make this loop until another button is pressed? Thanks.

Trevor,

When case: 22 goes to the wallAvoidance function, it starts Sparki moving forward. It activates sonar, then delays for 100 milliseconds. Since there is no loop, it goes back to case: 22 and breaks. After that, it is just going through the readIR/switch loop.

A better way to write this would be to add a boolean switch for the function:

case 22: if (dowallAvoidance = false) {
dowallAvoidance = true;
break;
}
else
{
dowallAvoidance = false)
}

Then:

sparki.updateLCD():
if (dowallAvoidance = true){
wallAvoidance();
}

Something along those lines. That way, wallAvoidance is part of the loop…

@cvpsmith:

Thanks a million. Worked almost straight out of the box. Wouldn’t compile, so I declared the dowallAvoidance as an int. Was this the right approach? Works now. Runs the wall avoidance continuously & takes commands from the remote. But, I’m not able to stop the wall avoidance loop with the remote. How can I do this?

Here’s the updated code:

//Based on 2 seperate sketches by Arcbotics (Remote & Wall Avoidance).
//www.arcbotics.com

//Updates by Trevor Moody
//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(); // clears lcd to prep for displaying list of buttons pressed
}

// map of button assignments on Sparki remote:

// /------^-----\
// |            |
// | 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();
            int dowallAvoidance;
            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();
           dowallAvoidance = false;
           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;
  
  // Assign Wall Avoidance to button 22  
  //Using a boolean switch to keep wallAvoidance function in loop mode if condition is met.
      case 22: if (dowallAvoidance = false) {
          dowallAvoidance = true; 
          break;
          }
              else
              {
              dowallAvoidance = false;
              }
              
              
      case 25: if (dowallAvoidance = true) {
          dowallAvoidance = false; 
          break;
          }        
    
      default:
      break;
  
  }
      
      
     
      
sparki.updateLCD();

        //Use this to keep wallAvoidance in a loop
        if (dowallAvoidance = true){
        wallAvoidance();
        }
        
          
}





  void wallAvoidance()
{
    sparki.servo(SERVO_CENTER); //place sonar at center position
    sparki.moveForward(); // move forward
    sparki.RGB(RGB_GREEN); // turn the light green
    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)


}

Thanks.

No prob…

The declaration should be outside the loop. Otherwise, it gets re-initialized on every loop. Also, I would declare doWallAvoidance as a boolean (it will work with an int but boolean makes better sense there)…

Like so:

//Based on 2 seperate sketches by Arcbotics (Remote & Wall Avoidance).
//www.arcbotics.com

//Updates by Trevor Moody
//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(); // clears lcd to prep for displaying list of buttons pressed
}

// map of button assignments on Sparki remote:

// /------^-----\
// |            |
// | 69  70  71 |
// | 68  64  67 |
// |  7  21   9 |
// | 22  25  13 |
// | 12  24  94 |
// |  8  28  90 |
// | 66  82  74 |
// \____________/

boolean dowallAvoidance;

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();
           dowallAvoidance = false;
           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;
  
  // Assign Wall Avoidance to button 22  
  //Using a boolean switch to keep wallAvoidance function in loop mode if condition is met.
      case 22: if (dowallAvoidance = false) {
          dowallAvoidance = true; 
          break;
          }
              else
              {
              dowallAvoidance = false;
              }
              
              
      case 25: if (dowallAvoidance = true) {
          dowallAvoidance = false; 
          break;
          }        
    
      default:
      break;
  
  }
      
      
     
      
sparki.updateLCD();

        //Use this to keep wallAvoidance in a loop
        if (dowallAvoidance = true){
        wallAvoidance();
        }
        
          
}





  void wallAvoidance()
{
    sparki.servo(SERVO_CENTER); //place sonar at center position
    sparki.moveForward(); // move forward
    sparki.RGB(RGB_GREEN); // turn the light green
    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)


}

Here is a general template that I use for programming Sparki:

#include <Sparki.h> // include the sparki library

void setup() 
{ 
  // Setup tasks
}

  // Initial Declarations (variables)

void loop() // Main Loop   
{  
  // Declarations within the scope of the loop.  Note:  these get re-initialized at the beginning of each loop.
  //   For example, if you take a look at the Line_Following example code, lineLeft gets re-declared at
  //   each loop and gets its value from sparki.lineLeft.

  // Main code  
}

  void function1()
{
  // First function called from main loop.
}

 void function2()
{
  // Second function, etc.
}

EDIT: Updated w/ code brackets… duh… :wink:

@cvpsmith:

Thanks again. Made changes as you suggested, but I’m still stuck in the wall avoidance loop. Still can’t quit by means of a remote button press. I’ll look again later, but it’s melting my head a little now :confused: