Hi all experts, I have a board Mega 2560 and I need to control 4 3-wire hobby servos.
1 button on the remote control - servo 1 to one side, button 2 - 1 servo back, button 3 - 2 servos to one side, press 4 - 2 servos back.
There are codes from an IR remote - I would use the button 1, 2 and up and down arrows for my servo control.
Up Arrow FF629D
Left Arrow FF22DD
OK FF02FD
Right Arrow FFC23D
Down Arrow FFA857
1 FF6897
2 FF9867
3 FFB04F
4 FF30CF
5 FF18E7
6 FF7A85
7 FF10EF
8 FF38C7
9 FF5AA5
FF42BD
0 FF4AB5
FF52AD
I can not find anywhere how to program multiple servos, always works for me only one servo - can you advise someone more experienced, I will be very happy.
I thank everyone beforehand for any help.
I see you also skipped the How to use this form thread Please edit your post and add code tags.
And I see you want to fade frome one side to the other. That makes everything a bit harder indeed. Let’s start by removing the delay (and never use it again) See, Blink without delay.
And btw, having a ccw and a cw variable is a but weird, especcilly because you set both different. Because now you can have that cw and ccw are both true at the same time, then what? I would say a cw (or ccw) variable and a run variable make more sens.
But like I said, arrays! Make an array of all the variables and objects you now have for one servo and make an array for them Now you can loop over all the servo’s
for(byte i = 0; i < sizeof(servos)/sizeof(servos[0]); i++){
//do the stuff for each servo
servos[i].write(angle[i]);
}
Or making it a bit more readable by adding this macro on top
#define elements(x) sizeof(x)/sizeof(x[0])
for(byte i = 0; i < elements(servos); i++){
//do the stuff for each servo
servos[i].write(angle[i]);
}
I:\Dokumenty\Arduino\sketch_sep07b\sketch_sep07b.ino: In function 'void loop()':
sketch_sep07b:27: error: 'servos' was not declared in this scope
for(byte i = 0xFF6897; i < elements(servos); i++){
^
I:\Dokumenty\Arduino\sketch_sep07b\sketch_sep07b.ino:3:28: note: in definition of macro 'elements'
#define elements(x) sizeof(x)/sizeof(x[0])
^
sketch_sep07b:29: error: 'angle' was not declared in this scope
servos[1].write(angle[0]);
^
sketch_sep07b:31: error: 'servos' was not declared in this scope
for(byte i = 0xFF9867; i < elements(servos); i++){
^
I:\Dokumenty\Arduino\sketch_sep07b\sketch_sep07b.ino:3:28: note: in definition of macro 'elements'
#define elements(x) sizeof(x)/sizeof(x[0])
^
sketch_sep07b:33: error: 'angle' was not declared in this scope
servos[1].write(angle[90]);
^
sketch_sep07b:33: error: expected '}' at end of input
servos[1].write(angle[90]);
^
sketch_sep07b:33: error: expected '}' at end of input
#include <IRremote.h> //must copy IRremote library to arduino libraries
#include <Servo.h> //must copy IRremote library to arduino libraries
#define elements(x) sizeof(x)/sizeof(x[0])
int RECV_PIN = 19; //IR receiver pin
Servo servo1; // 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
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
servo1.attach(9); //servo1 pin
}
void loop()
{
for(byte i = 0xFF6897; i < elements(servos); i++){
//do the stuff for each servo
servos[1].write(angle[0]);
}
for(byte i = 0xFF9867; i < elements(servos); i++){
//do the stuff for each servo
servos[1].write(angle[90]);