Go Down

Topic: How do I stop an Arduino sketch? (Read 1 time) previous topic - next topic

welchsc

I am able to use the Arduino sweep servo example sketch.  I can't for the life of me figure out how to end the for loop causing the program to stop.  Even if I close the Arduino IDE down it continues to run.  If I unplug the usb cable and plug it back in it continues to run the sweep program even without the Arduino IDE app open?  It seems some service or executable is still running.  Can someone please tell me how to stop the program.  Maybe even through the serial monitor interface??
I would even like to rewrite the program to just move the servo a couple of degrees and then stop/pause until I tell it to move further.  Thanks in advance.

Osgeld

the arduino is its own little computer, it starts the sketch and continues to run whatever is in void loop() forever unless you specifically program it to do otherwise
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

CrossRoads

"I would even like to rewrite the program to just move the servo a couple of degrees and then stop/pause until I tell it to move further. "
That's how you stop it - let it do an action after it sees a button push, then it goes back to reading the pin/port ready to act on another button push.
Or reading a serial port. Or taking analog samples.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

welchsc

Could you direct me to a sketch that doesn't use a loop but rather moved the servo to a specified degree?  I am unable to find any such code.  Or can you tell me how to modify the example sweep code to stop the loop.  I am remoting into my pc from work and controlling a webcam attached to the top of the servo from across the net.  This is why I don't want it to continually move.

PaulS

Quote
This is why I don't want it to continually move.

Good idea.

Quote
Could you direct me to a sketch that doesn't use a loop but rather moved the servo to a specified degree?  I am unable to find any such code.

This implies that you haven't bothered to try to understand the sketch that you have. If you don't want something to happen in a loop, don't put the action inside a loop. I'm pretty sure that with a little study (and I do mean a little) you'll be able to distinguish the loop part and the action part. Then, remove the one you don't want.

welchsc

I do understand the premise behind loops.  I just can't find code anywhere on the net that would allow me make the servo move to a specified degree then stop so it can easily be controlled over the net through remogte access.  Is the Arduino code Java or C+?  I was also under the impression that I could type code in the serial monitor to make the servo move accordingly.  Sorry for the newbie questions.  If there were a primer I could reference I would be more than happy to look at it.

Arrch


I just can't find code anywhere on the net that would allow me make the servo move to a specified degree then stop so it can easily be controlled over the net through remogte access.

With such a specific requirement, it's no surprise that the exact code you're looking for isn't readily available.
Quote
Is the Arduino code Java or C+?

Neither, it's C++.
Quote
I was also under the impression that I could type code in the serial monitor to make the servo move accordingly.

Not sure what gave you that impression, the sweep example has no Serial communications code in it, what-so-ever.
Quote
If there were a primer I could reference I would be more than happy to look at it.

There are dozens of example sketches you can look at that already come with the Arduino IDE.

cantore

you can do what you want in the "Setup" if you want to do it only once.

CrossRoads

Take a look at the
Examples:Servo:Knob code in the IDE:

Code: [Select]

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}


This reads a potentiometer, and moves the servo based on the reading.
Do the same but get the reading from your remote connection instead.

Code: [Select]

void loop(){
// check outside source, see if new position came in
if (new position came in && new position is within allowed limits){
// move the servo - say 5 degrees left or 5 degrees right depending on the command  - all depends on the protocol you set up.
}
else {
// do not move servo
}
} // end loop
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up