I did read about arrays and I like to do it myself, if I would be able to....
Here is one of my aproaches, but that seems to be not the right way to do it. I just can't figure it out
#include <Servo.h>
int num;
Servo servo_1; // create servo object to control a servo
Servo servo_2; //------------- how do array these????
Servo servo_3;
Servo servo_4;
int pos_1; // variable to store the servo position
int pos_2;
int pos_3;
int pos_4;
unsigned long previousMillis = 0;
unsigned long previousMicros_1 = 0;
unsigned long previousMicros_2 = 0;
unsigned long previousMicros_3 = 0;
unsigned long previousMicros_4 = 0;
long interval = 1000;
const int ledPin = 13; // the number of the LED pin
int ledState = LOW;
int min_[] = {0,1,2,3,4};
int max_[] = {0,1,2,3,4};
int speed_[] = {0,1,2,3,4};
int pos_[] = {0,1,2,3,4};
//---------------------------| SERVO_01 |-----------------------------------------------
void Servo_X(int num,int min_1, int max_1, int speed_1)
{
if ((micros()-previousMicros_1) >=speed_1) {
previousMicros_1 = micros();
if (pos_[num] > servo_[num].readMicroseconds()) {
servo_[num].writeMicroseconds([servo_[num]).readMicroseconds() + 1);
}
if (pos_[num] < servo_[num].readMicroseconds()) {
servo_[num].writeMicroseconds(servo_[num] .readMicroseconds() - 1);
}
if(servo_[num].readMicroseconds() == max_[num]) {
pos_[num] = min_[num];
}
if(servo_[num].readMicroseconds() <= min_[num]) {
pos_[num] = max_[num];
}
}
}
with this pretty error:
In function 'void Servo_01(int, int, int, int)':
error: request for member 'writeMicroseconds' in 'servo_[num]', which is of non-class type 'int
I woudn't ask for helf if I wouldn't really need it.
[edit]
why does this code from the night rider work?
isn't it similar to it? exept the " .readMicroseconds"
for (count=5;count>=0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer);
}
I searched so much already for a explaination where I can learn how to use structures, but wasn't able to find any. do you know a helpful link or do you have a good and simpel explainationfor them that I can do them by my self?
If you add [] after the variable name, it is an array.
You've allready assigned an array of values.. you've used this for making an array of numbers (integers).
It really works the same way with other variables, objects and structures aswell.
But instead of putting numbers in a comma seperated list, in between curly brackets, you list the values, this would be objects for objects and structures.. same principle though.
Servo servos[] = {Servo(), Servo(), Servo()}; //create 3 instances of the servo class
That is a very valid declaration, what bothers me is why you didn't thought of that? :-/
No offense, but you allready had the answer to your question yourself
#include <Servo.h>
Servo servo_1; // create servo object to control a servo
Servo servo_2;
Servo servo_3;
Servo servo_4;
int pos_1; // variable to store the servo position
int pos_2;
int pos_3;
int pos_4;
unsigned long previousMillis_1 = 0;
unsigned long previousMicros_1 = 0; // the variables is a long because the time, measured in miliseconds,
unsigned long previousMicros_2 = 0; // will quickly become a bigger number than can be stored in an int.
unsigned long previousMicros_3 = 0;
unsigned long previousMicros_4 = 0; // will store last time servo was updated
int servospeed = 900;
long interval = 1000;
const int ledPin = 13; // the number of the LED pin
int ledState = LOW;
[glow]servo_[] = {0, servo_1, servo_2, servo_3, servo_4};[/glow]
min_[] = {0, min_1, min_2, min_3, min_4};
max_[] = {0, max_1, max_2, max_3, max_4};
speed_[] = {0, speed_1, speed_2, speed_3, speed_4};
//---------------------------| SERVO_01 |-----------------------------------------------
void ServoX(int num, int min_1, int max_1, int speed_1)
{
if ((micros()-previousMicros_1) >=speed_[num]) {
previousMicros_1 = micros();
if (pos_1 > servo_[num].readMicroseconds()) {
servo_[num].writeMicroseconds(servo_[num].readMicroseconds() + 1);
}
if (pos_1 < servo_[num].readMicroseconds()) {
servo_[num].writeMicroseconds(servo_[num].readMicroseconds() - 1);
}
if(servo_1.readMicroseconds() == max_[num]) {
pos_1 = min_[num];
}
if(servo_1.readMicroseconds() <= min_[num]) {
pos_1 = max_[num];
}
}
}
void setup()
{
servo_1.attach(9); // attaches the servo on pin 9 to the servo object
servo_2.attach(10);
servo_3.attach(11);
servo_4.attach(12);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
ServoX(1,700,2200,1000);
if (millis() - previousMillis_1 > interval) {
// save the last time you blinked the LED
previousMillis_1 = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
Serial.println(millis());
Serial.println();
}
}
error: expected constructor, destructor, or type conversion before '=' token In function 'void ServoX(int, int, int, int)':
So I changed it so that there is no error anymore. the "0" was just so that I can type for the servos 1,2,3,4 instead of 0,1,2,3 but I erased that.
the yellow marked places are the changed ones. I tried to but the ints into the arry but that didn't seem to work. It then says expected primary expression before int.
#include <Servo.h>
[glow]Servo servo_1; // create servo object to control a servo
Servo servo_2;
Servo servo_3;
Servo servo_4;
int min_1;
int min_2;
int min_3;
int min_4;
int max_1;
int max_2;
int max_3;
int max_4;
int speed_1;
int speed_2;
int speed_3;
int speed_4;
int pos_1; // variable to store the servo position
int pos_2;
int pos_3;
int pos_4;[/glow]
unsigned long previousMillis_1 = 0;
unsigned long previousMicros_1 = 0; // the variables is a long because the time, measured in miliseconds,
unsigned long previousMicros_2 = 0; // will quickly become a bigger number than can be stored in an int.
unsigned long previousMicros_3 = 0;
unsigned long previousMicros_4 = 0; // will store last time servo was updated
int servospeed = 900;
long interval = 1000;
const int ledPin = 13; // the number of the LED pin
int ledState = LOW;
[glow]Servo servo_[] = {servo_1, servo_2, servo_3, servo_4};
int min_[] = {min_1, min_2, min_3, min_4};
int max_[] = {max_1, max_2, max_3, max_4};
int speed_[] = {speed_1, speed_2, speed_3, speed_4};
[/glow]
//---------------------------| SERVO_01 |-----------------------------------------------
void ServoX(int num, int min_1, int max_1, int speed_1)
{
if ((micros()-previousMicros_1) >= speed_[num]) {
previousMicros_1 = micros();
if (pos_1 > servo_[num].readMicroseconds()) {
servo_[num].writeMicroseconds(servo_[num].readMicroseconds() + 1);
}
if (pos_1 < servo_[num].readMicroseconds()) {
servo_[num].writeMicroseconds(servo_[num].readMicroseconds() - 1);
}
if(servo_1.readMicroseconds() == max_[num]) {
pos_1 = min_[num];
}
if(servo_1.readMicroseconds() <= min_[num]) {
pos_1 = max_[num];
}
}
}
void setup()
{
servo_1.attach(9); // attaches the servo on pin 9 to the servo object
servo_2.attach(10);
servo_3.attach(11);
servo_4.attach(12);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
ServoX(1,700,2200,1000);
if (millis() - previousMillis_1 > interval) {
// save the last time you blinked the LED
previousMillis_1 = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
Serial.println(millis());
Serial.println();
}
}
here is a fragment showing how to declare and use an array of servos
#include <Servo.h>
const int servoCount = 4;
Servo servos[servoCount];
const int pins[servoCount] = {9,10,11,12};
void setup() {
for(int i=0; i < servoCount; i++)
servos[i].attach(pins[i]);
}
void writeServo(int index, int value) {
servos[index].write(value);
}
void loop() {
writeServo(0, 90); // write a value of 90 to the first servo
delay(20);
}