Hello all, trying to create an action loop for a device. The premises-
device is listening to two dmx inputs, which is all declared and figured out in the main file.
based on the input it gets it activates a servo motor thats listening to pin 9.
if the first dmx signal 0 is higher than 127 it exectutes the first sequence only once.
if the second dmx signal 1 is higher than 127 it executes the second sequence only once.
when it gets the command to activate a servo it also turns on an LED for a bit and turns it off on its own regardless of the commands.
if dmx input is less than 127 on either dmx input, 0 or 1, it should do nothing and keep the LED off.
Once it executes a servo sequence it needs to wait for more commands and do those when it gets them.
If by chance it gets the first and second dmx signal together at any value it should do nothing.
Heres is the code:
void action() {
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
//begin code
for(byte j = 0; j < NUMBER_OF_CHANNELS; j++) {
Serial.print(dmxvalue[j]);
}
myservo.attach(9); // attaches the servo on pin 9 to the servo object
if(dmxvalue[0] > 127) {
digitalWrite(13, HIGH);
delay(5000);
digitalWrite(13, LOW);
for(pos = -100; pos <= 30; pos += 1) // goes from -100 degrees to 30 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(55); // waits 55ms for the servo to reach the position
}
for(pos = 30; pos>= -70; pos-=1) // goes from 30 degrees to -70 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
while(1){} //do not repeat the action
}
if(dmxvalue[1] > 127) {
digitalWrite(13, HIGH);
delay(5000);
digitalWrite(13, LOW);
for(pos = 30; pos <= 100; pos += 1) // goes from 30 degrees to 100 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(55); // waits 55ms for the servo to reach the position
}
for(pos = 100; pos>= -70; pos-=1) // goes from 100 degrees to -70 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
while(1){} //do not repeat the action
}
else
{
digitalWrite(13, LOW);
}
return; //go back to loop()
}
//end action() loop
The code compiles ok, and I got the LED to turn on when it gets a signal and turn off on its own, however the sequence needs to be a loop because no consequential commands are taken and until reset no commands are understood. I have tried to add void loop () to the code, but it gives me errors. The servo is generally unhappy as well and doesnt seem to correctly respond, but it does respond. Not sure how to phrase the "if both dmx signals are present do nothing" either.
No actually all of it compiles and uploads fine, see the entire packet attached. Code is old and written to work with the 0023 IDE. HardwareSerial.cpp of the IDE must be replaced with the one provided in the packet to make DMX work. The original HardwareSerial gets renamed with a .backup name.
For now I defined the number of channels as 8 in case I need them to do other servo sequences.
The main file has a void loop of its own, I suppose it doesnt like a loop within a loop maybe. However there needs to be a loop thats looking for a change in the dmx values received and a loop that tracks what the changes are. If that makes sense.
Angie221:
if the first dmx signal 0 is higher than 127 it exectutes the first sequence only once.
if the second dmx signal 1 is higher than 127 it executes the second sequence only once.
when it gets the command to activate a servo it also turns on an LED for a bit and turns it off on its own regardless of the commands.
if dmx input is less than 127 on either dmx input, 0 or 1, it should do nothing and keep the LED off.
Once it executes a servo sequence it needs to wait for more commands and do those when it gets them.
If by chance it gets the first and second dmx signal together at any value it should do nothing.
Sorry, but I can't relate that to the code in your action() function.
In addition to @KenF's comment I see that you have a whopping great delay(5000) in the code.
And you seem to be sending negative values to the servo.
You are creating the Servo instance in the function and attaching the servo. Both should be in setup() - or at least should be isolated from the regular calls to the action() function.
That's not why I posted the link. The other thread will maybe help as it references the entire code being used and you would see the action function is a separate tab, hence no setup or loop in posters code shown here.
Maybe they would take more notice if the OP asked for them to be merged.
No. The problem is that the new version of the forum makes merging threads difficult, if not impossible, unless they are in the same part of the forum.
My goodness, I didnt' realize I broke the internet too...
I will move creating the servo to setup and see if that helps. The negative values sent to the servo worked when I had the servo run just on its own in a simple setup without any fancy programming...but now I'm questioning if something else was helping it.
The while[1]{} is partly intentional as I was under the impression that in this case it would prevent the servo from continuously executing its two commands because I use a for function. I didnt think it would bring the whole thing to a halt, which is not desired. I will take that line out and see what happens. Will it need some other way of knowing to stop after going through its two movements?
As far as the 5000 delay it was a number that seemed to make sense at the time. Most of the light cues that trigger the devices are also 5 sec long.
Angie221:
Will it need some other way of knowing to stop after going through its two movements?
When you command a servo with (e.g.) servo.write(125); it moves to the angle of 125 deg and stays there until you give it another command. There is no need to stop anything.
Robin2:
When you command a servo with (e.g.) servo.write(125); it moves to the angle of 125 deg and stays there until you give it another command. There is no need to stop anything.
...R
Or perhaps more correctly, we think "it moves to the angle of 125 deg and stays there", which assumes it actually got there in the first place.... if it didn't actually get there for whatever mechanical reason like not being torquey enough, it will continue to try to get there due to its own internal feedback mechanism but it does not feed that fact back to us as the users.
Also just for clarity, "another command" includes a myServo.detach(), which has the effect of stopping the continuous 50Hz pulsing of the required position.