I know it's not pretty, but it's the first typed version, so forgive my sloppiness. Final version will be cleaned up.
I've gotten rid of the const int, I've moved all my functions around, prototyped them, changed them to voids, etc, and I always get
In function 'void setup()':
error: a function-definition is not allowed here before '{' token
Though it usually highlights the { for void loop () and not void setup ()
// Copyright 2007 Matthew Pace
// Animatronic drummer
// This program will start by obtaining 8 values
// 4 for each servo. The values will represent
// as a pair a set location for the arm in 3d
// space. It will then loop randomly choosing
// one of teh three spaces and drumming with it
const int drum_count = 4; // the number of drums we have
int pot_pin = 0; // the potentiometer used to set locations
int switch_pin = 11; // an on/off momentary switch
int servo1 = 9; // the pin used for signalling the servo that controls horizontal motion
int servo2 = 6; // the pin used for signalling the servo that controls vertical motion
int servo1_points [drum_count]; //an array of servo positions
int servo2_points [drum_count];
int cur_drum ; //the drum we are currently hitting
void hit_drum () ;
void setup ()
{
pinMode (13, OUTPUT); // well since freeduino and decimillia have an LED built in, why not
pinMode(switch_pin, INPUT);
int i = 0; //for loop use and addressing the arrays of servo positions
int pot;
while (i < drum_count)
{
while (digitalRead(switch_pin) == HIGH) //we're using a pull up resistor, so it's normally high
{
pot=analogRead(pot_pin); // store the value of the potentiometer in pot
}
//when we get here, it means the momentary switch was hit just last loop
servo1_points [i] =(int) (500*(pot/1023))+1250; //store the pot value converted into servo pwm in it's spot
digitalWrite(13, HIGH); //let us know it read it via the included LED
delay (250); //pause for a quarter second
digitalWrite (13, LOW);
while (digitalRead(switch_pin) == HIGH) //we're using a pull up resistor, so it's normally high
{
pot=analogRead(pot_pin); // store the value of the potentiometer in pot
}
//when we get here, it means the momentary switch was hit just last loop
servo2_points [i] = (int) (500*(pot/1023))+1250; //store the pot value converted into servo pwm in it's spot
digitalWrite(13, HIGH); //let us know it read it via the included LED
delay (250); //pause for a quarter second
digitalWrite (13, LOW);
randomSeed (analogRead(0));
}
//that concludes the setup. at this point
//we have 2 arrays, defining points for
//2 servos
void hit_drum ();
{
int last_1 = millis();
int last_2 = millis();
int i = 20;
digitalWrite(servo1, HIGH);
digitalWrite(servo2, HIGH);
boolean waiting1 = true;
boolean waiting2 = true;
// since we are controlling 2 servos, we can't send a pulse and then delay 20 ms, as the other
// servo might get confused
while ( i > 0 )
{
if (waiting1 && millis()-last_1 >= servo1_points[cur_drum]) // if the current millis minus the time of the last time we
{ // sent a signal to servo1 is >= time to hold it high
digitalWrite(servo1, LOW); // AND we are waiting for it to finish with its high signal
last_1 = millis();
waiting1=false;
}
if (waiting2 && millis()-last_2 >= servo2_points[cur_drum]) // if the current millis minus the time of the last time we
{ // sent a signal to servo1 is >= time to hold it high
digitalWrite(servo2, LOW); // AND we are waiting for it to finish with its high signal
last_1 = millis();
waiting2=false;
}
if(!waiting1 && millis()-last_1 >= 20000)
{
digitalWrite(servo1, HIGH);
i++;
last_1 = millis();
}
if(!waiting2 && millis()-last_2 >= 20000)
{
digitalWrite(servo2, HIGH);
last_2 = millis();
}
}
return ;
}
void loop()
{
cur_drum = random (0, drum_count); //won't overflow the array, as arrays start from 0, and random's max is exclusive
hit_drum (cur_drum);
}