I am doing a small project for disabled person (guitar playing tool).
I have a problem. I have got a code that is done for one servo and I need to control 6 servos in the same way.
I am a beginner in programming. I have no idea where to start and I have two days to do it.
My attepts were quite bad.
Have you got any helpful advices good people?
Huge thanks for instrest.
Simon.
Code:
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
const int buttonPin = 2; // the number of the pushbutton pin
int servoPosition;
void changePosition(){
}
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
servoPosition = 30;
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
int prev = HIGH;
int state = HIGH;
void loop()
{
prev = HIGH;
state = digitalRead(buttonPin);
while((state == HIGH && state == prev)){
state = digitalRead(buttonPin);
prev = state;
}
Serial.print(state);
if(servoPosition == 30){
servoPosition = 150;
} else {
servoPosition = 30;
}
myservo.write(servoPosition);
delay(1000);
state = digitalRead(buttonPin);
while((state == LOW && state == prev)){
state = digitalRead(buttonPin);
prev = state;
}
}
Hi Simon. Can you explain what the sketch is supposed to do. I can somewhat guess by reading the sketch but need more clarity from you. Will there be 6 buttons also?
I have checked this has compiled OK, but further than that I cannot test it for you.
#include <Servo.h>
#define servoCount 6
Servo myservo[servoCount];// create servo object to control a servo
// a maximum of eight servo objects can be created
const int buttonPin[servoCount] = {2, 4, 5, 6, 7, 8}; // the number of the pushbutton pin
const int servoPin[servoCount] = {9, 10, 11, 12, 13, 3}; // the number of the servo pin
int servoPosition[servoCount] = {30, 30, 30, 30, 30, 30}; // variable to store the servo position
int prev[servoCount] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
void setup() {
for (int s = 0; s < servoCount; s++) {
myservo[s].attach(servoPin[s]);
myservo[s].write(servoPosition[s]);
pinMode(buttonPin[s], INPUT_PULLUP);
}
Serial.begin(9600);
}
void loop() {
for (int s = 0; s < servoCount; s++) {
int state = digitalRead(buttonPin[s]);
if (state != prev[s]) {
Serial.print("Button ");
Serial.print(s);
Serial.print(" state = ");
Serial.println(state);
if(prev[s] != HIGH) {
if(servoPosition[s] == 30){
servoPosition[s] = 150;
}
else {
servoPosition[s] = 30;
}
myservo[s].write(servoPosition[s]);
}
prev[s] = state;
}
}
}