Pages: [1]   Go Down
Author Topic: How do I stop an Arduino sketch?  (Read 1036 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45960
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

italy
Offline Offline
Full Member
***
Karma: 2
Posts: 215
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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:
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
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: