servo

so i'am making a change machine for a little arcade i have in my room and i have a coin mechanism that when one quarter is inserted it dispenses one token i'am using the sweep example and i'am wondering how can i make it not loop like the servo only moves once forward and once backward then stops when one coin is inserted

Can't give advice without seeing your code.

...R

Taking the sweep example as a starting point, move everything in its loop() function into a separate function. Then in the loop function read the input indicating that a coin has been inserted and call the new function.

const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag

void setup()
{
Serial.begin(9600);
//Start Serial Communication
attachInterrupt(coinInt, coinInserted, RISING);
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()
//The function that is called every time it recieves a pulse
{
coinsValue = coinsValue + 0.05;
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
coinsChange = 1;
//Flag that there has been a coin inserted
}

void loop()
{
if(coinsChange == 1)
//Check if a coin has been Inserted
{
coinsChange = 0;
//unflag that a coin has been inserted

if(coinsValue >= 0.25)
{
//your code here, eg Big Flashing Lights!!!! and Serial.println("I'M MAKING MONEY!!!!");
}

where it says your code here i want a servo to move 60 and back 60 then end

also i get this error
sketch_nov03a.ino: In function 'void loop()':
sketch_nov03a:40: error: expected }' at end of input sketch_nov03a:40: error: expected }' at end of input

I still suggest that you write a function to move the servo as you want and call it from where you have inserted the comment. You can base the function on the servo sweep example with the limits adjusted to suit your requirements.

As to the error messages. Where is the end of the loop() function ? What does every function end with ?

There is no servo stuff in the code you posted.

You seem to be missing a } on line 35.

Where you have

//unflag that a coin has been inserted!

put a call to your servo function - which you have yet to create - so it dispenses the token.

Please post you code properly using code tags so it looks like the above. It makes it much easier to work with.

The servo function might be as simple as

void dispenseToken() {
   myServo.write(90);
   delay(100);
   myServo.write(10);
}

...R

i inserted that simple servo code you gave me and i got this error

sketch_nov03a:2: error: expected declaration before '}' token

rypants:
i inserted that simple servo code you gave me and i got this error

sketch_nov03a:2: error: expected declaration before '}' token

Where did you insert it ? Please post the whole program with it in place.

rypants:
i inserted that simple servo code you gave me and i got this error
sketch_nov03a:2: error: expected declaration before '}' token

What I gave you was a function. It should have been included after the rest of your code. Then you would call the function with the line

dispenseToken();

which should probably be put where you put the function.

The advantage of using a function is that the call to it explains exactly what will happen without needing to look at the code in the function.

See the Thread planning and implementing a program

...R

ok im petty sure i jacked up the whole program

const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag

void setup()
{
Serial.begin(9600);
//Start Serial Communication
attachInterrupt(coinInt, coinInserted, RISING);
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()
//The function that is called every time it recieves a pulse
{
coinsValue = coinsValue + 0.05;
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
coinsChange = 1;
//Flag that there has been a coin inserted
}

void loop()
{
if(coinsChange == 1)
//Check if a coin has been Inserted
{
coinsChange = 0;
//unflag that a coin has been inserted

if(coinsValue >= 0.25)

void dispenseToken()
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

myServo.write(90);
delay(100);
myServo.write(10);
}
these are the errors

Arduino: 1.0.6 (Windows XP), Board: "Arduino Uno"
sketch_nov03a.ino: In function 'void loop()':
sketch_nov03a:41: error: expected initializer before 'Servo'
sketch_nov03a:45: error: 'myServo' was not declared in this scope
sketch_nov03a:48: error: expected `}' at end of input

const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag

void setup()
{
Serial.begin(9600);
//Start Serial Communication
attachInterrupt(coinInt, coinInserted, RISING);
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()
//The function that is called every time it recieves a pulse
{
coinsValue = coinsValue + 0.25;
//As we set the Pulse to represent 25p or 25c we add this to the coinsValue
coinsChange = 1;
//Flag that there has been a coin inserted
}

void loop()
{
if(coinsChange == 1)
//Check if a coin has been Inserted
{
coinsChange = 0;
//unflag that a coin has been inserted

if(coinsValue >= 0.25)
{
this is where i want the servo code to be

}
}
}
so i got this code working with no errors but i still don't quite under stand how to get that servo code in i put in a servo function and it says you need to declare servo i put a decoration in and it says this

Arduino: 1.0.6 (Windows XP), Board: "Arduino Uno"
sketch_nov04a.ino: In function 'void loop()':
sketch_nov04a:39: error: 'Servo' was not declared in this scope
sketch_nov04a:39: error: expected `;' before 'myservo'
sketch_nov04a:45: error: a function-definition is not allowed here before '{' token``

You reference coinsChange in the interrupt service routine and in loop(). It needs to be volatile for that to work properly.

i put in a servo function

Where? What function?

and it says you need to declare servo i put a decoration in

Where? What decoration/declaration?

and it says this

How is the compiler supposed to know what a Servo is? You need to include the header file for the class, which I'm guessing that you didn't do.

all i need is the servo function (servo move 60 forward 60 backward when 0.25c is inserted) to go where the your code here and i don't know what a header file is

i don't know what a header file is

In your first post you indicated that you were using the Servo Sweep example program. What is the first line in that program after the comments at the top ?

Jeez it is hard to see how this could get mixed up. What I meant in Reply #6 is the following. I have marked the new bits with "<----------------"

I have compiled it successfully but I have not tried it.

#include <Servo.h>   // <--------------------servo library
Servo myServo;       // <--------------------servo instance

const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;                 
//A Coin has been inserted flag

void setup()
{
	  Serial.begin(9600);                 
	//Start Serial Communication
	  attachInterrupt(coinInt, coinInserted, RISING);   
	//If coinInt goes HIGH (a Pulse), call the coinInserted function
	//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()   
	//The function that is called every time it recieves a pulse
	{
	  coinsValue = coinsValue + 0.05; 
	//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
	  coinsChange = 1;                           
	//Flag that there has been a coin inserted
}

void loop()
{
	  if(coinsChange == 1)         
	//Check if a coin has been Inserted
	  {
		coinsChange = 0;             
	//unflag that a coin has been inserted
		dispenseToken();   // <------------------ new function call
	  }  
	   
	  if(coinsValue >= 0.25)
	  {
			  //your code here, eg Big Flashing Lights!!!! and Serial.println("I'M MAKING MONEY!!!!");
	  }
}

void dispenseToken() {    // <------------------ new function
   myServo.write(90);
   delay(100);
   myServo.write(10);
}

...R

I'd be inclined to drop the use of "float" and count whole cents (pennies, whatever).

all i need is the servo function

There is no servo function. There is a Servo CLASS that has some useful methods. The class is defined in a header file (with a .h extension). In order to create an instance of the Servo class you need to include the header file:

#include <Servo.h>

What pin is the a
Servo connected to in robins post

It doesn't appear to be attached to any pin, so you can pick one yourself.