Continuous servo memory

Great idea! Thanks ScruffR!

So, just for posterity… incase anyone else wants to copy… here’s the final version.

Servo timservo; 

int position = 0;    // variable to store the servo position
int newposition = 0;
 
void setup()
{

//Setup the Servo to the function 
Particle.function("timservo", updatetimServo);
//Set pin D0 to be an output
pinMode(D0, OUTPUT);

//Take control over the LED on the board
RGB.control(true);

//There may be a better way to do this like turning it off, I found it just as easy to set the colour to nothing
RGB.color(0, 0, 0);
}
 
void loop()
{

 
}
 
//This function is triggered by IFTTT - the 'command' word represents the object used to store the 'position' we send to the function.
//The 'position' we send represents where we want the servo to move to
int updatetimServo(String command)
{

newposition = command.toInt();

// 0deg
    
  if (newposition == 1) {
    switch(position) {
      case 2:
		timservo.write( 100 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 3:
		timservo.write( 100 ); 
		delay( 2000 ); 
		timservo.write( 90 );
        break;
      case 4:
		timservo.write( 100 ); 
		delay( 3000 ); 
		timservo.write( 90 );
      default:
        return -1;
    }
  }

// 45deg

  if (newposition == 2) {
    switch(position) {
      case 1:
		timservo.write( 80 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 3:
		timservo.write( 100 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 4:
		timservo.write( 100 ); 
		delay( 2000 ); 
		timservo.write( 90 );
      default:
        return -1;
    }
  }
  
// 90deg
    
  if (newposition == 3) {
    switch(position) {
      case 1:
		timservo.write( 80 ); 
		delay( 2000 ); 
		timservo.write( 90 );
        break;
      case 2:
		timservo.write( 80 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 4:
		timservo.write( 100 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      default:
        return -1;
    }
  }

// 135deg
  
    if (newposition == 4) {
    switch(position) {
      case 1:
		timservo.write( 80 ); 
		delay( 3000 ); 
		timservo.write( 90 );
        break;
      case 2:
		timservo.write( 80 ); 
		delay( 2000 ); 
		timservo.write( 90 );
        break;
      case 3:
		timservo.write( 80 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      default:
        return -1;
    }
  }
  
  position = newposition;
  return position;
  
}

Now that you’ve got that working, try moving the logic from the function handler to the loop, since it’s bad practice (and often problematic) to have functions block for a long time. Try keeping them as short as possible.

That’s a great idea Moors7. Not sure where to begin, but I guess that’s part of the fun. I’m sure I can’t be the only one who struggles with these things. Are there any good learning materials out there? I’ve been through the documentation on the particle.io site but that only takes me so far.

We’ve all been there (heck, I’m still there ;)). I personally like to learn by picking a project and figuring things out as I go. Then you do something that’s useful to you, so it’s easier to put in the effort. I know there are some books out there with several beginner project that might help get you started. Simon Monk has written one that I know of. That said, I haven’t tried any of those, so no guarantees as to what’s in there.

I agree on the figuring out thing. My only issue is that once something works, often I have no idea why it works. I certainly, more often than not, have no idea how to then improve upon it (as you suggested above). I’ve done this one by going backwards and forwards through this forum picking up on bits (and lots of help from nice people). It’s the basics of ‘how to send a function to the loop’ that I just don’t have a handle on.

I’d love to work out how stepper motors work too, but there doesn’t seem to be any literature out there with just a basic, “buy this, do this… see, it works and here’s why”… so I have literally no idea where to begin. Does that make sense?

The way you currently use the function is to send it a new variable, and then immediately check/act upon that variable. Rather than doing that ‘hard work’ in the function, simply check for it in the loop.

Some extremely simplified code would be:

loop() {
  if (should_I_do_the_thing == true){
    do_the_thing();
    should_I_do_the_thing = false;
  }
}

//function call
int thing_function(String command){
  should_I_do_the_thing = true;
  return 1;
}

Thanks Moors7, I think that helped a lot.

So, it would look like this? (seems to verify ok… which I find shocking!!)

Servo timservo; 

int position = 0;    // variable to store the servo position
int newposition = 0;
bool makeachange = false;
 
void setup()
{

//Setup the Servo to the function 
Particle.function("timservo", updatetimServo);
//Set pin D0 to be an output
pinMode(D0, OUTPUT);

//Take control over the LED on the board
RGB.control(true);

//There may be a better way to do this like turning it off, I found it just as easy to set the colour to nothing
RGB.color(0, 0, 0);
}
 
void loop()
{
    
if ( makeachange == true) {
    
// 0deg
    
  if (newposition == 1) {
    switch(position) {
      case 2:
		timservo.write( 100 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 3:
		timservo.write( 100 ); 
		delay( 2000 ); 
		timservo.write( 90 );
        break;
      case 4:
		timservo.write( 100 ); 
		delay( 3000 ); 
		timservo.write( 90 );
        break;
    }
  }

// 45deg

  if (newposition == 2) {
    switch(position) {
      case 1:
		timservo.write( 80 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 3:
		timservo.write( 100 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 4:
		timservo.write( 100 ); 
		delay( 2000 ); 
		timservo.write( 90 );
        break;
    }
  }
  
// 90deg
    
  if (newposition == 3) {
    switch(position) {
      case 1:
		timservo.write( 80 ); 
		delay( 2000 ); 
		timservo.write( 90 );
        break;
      case 2:
		timservo.write( 80 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
      case 4:
		timservo.write( 100 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
    }
  }

// 135deg
  
    if (newposition == 4) {
    switch(position) {
      case 1:
		timservo.write( 80 ); 
		delay( 3000 ); 
		timservo.write( 90 );
        break;
      case 2:
		timservo.write( 80 ); 
		delay( 2000 ); 
		timservo.write( 90 );
        break;
      case 3:
		timservo.write( 80 ); 
		delay( 1000 ); 
		timservo.write( 90 );
        break;
    }
  }
  
  position = newposition;
  makeachange = false;

}
}
 
//This function is triggered by IFTTT - the 'command' word represents the object used to store the 'position' we send to the function.
//The 'position' we send represents where we want the servo to move to
int updatetimServo(String command)
{
    
 newposition = command.toInt();
 makeachange = true;
 return position;
  
}
1 Like