I have two simple tasks I am using an arduino Yun to do. The first is light an RGB led, and the second is turn a servo motor.
I have the code working for both functions, but I am having trouble making them work together on the same arduino.
I read that using when the delay function is being used the arduino can not do two tasks, so i re did the servo code using millis.
This is the RGD LED code:
const int greenLEDPin = 11;
const int redLEDPin = 10;
const int blueLEDPin = 8;
int fadeAmount = 1;
int redValue = 20;
int greenValue = 20;
int blueValue = 20;
void setup() {
// put your setup code here, to run once:
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(redLEDPin, redValue);
analogWrite(blueLEDPin, blueValue);
analogWrite(greenLEDPin, greenValue);
Serial.begin(9600);
Serial.print(redValue );
Serial.print(greenValue );
Serial.print(blueValue );
Serial.println("");
//redValue = redValue + 1;
//blueValue = blueValue + 1;
//greenValue = greenValue + 1;
redValue = redValue + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (redValue == 0 || redValue == 255) {
fadeAmount = -fadeAmount ;
}
blueValue = blueValue + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (blueValue == 0 || blueValue == 255) {
fadeAmount = -fadeAmount ;
}
greenValue = greenValue + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (greenValue == 0 || greenValue == 255) {
fadeAmount = -fadeAmount ;
}
}
This is the servo code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo, max = 8 servos
int myservo_pin = 9; // pin that controls the servo
int myservo_speed = 1; // how fast the servo moves, 1 = very fast, 10 = very slow
long myservo_movetime = 10; // next time in millis servo next moves
int myservo_gPos = 0; // target position to move towards
int myservo_cPos = 0; // current postion of servo
int pos = 0; // variable to store the servo position
int cPos; // current position
int gPos; // goal position
int tDelay = 200; // delay between moves, gives appearance of smooth motion
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.println("setup complete : smooth servo movment without delay v1");
}
void loop() {
cPos = myservo.read();
if (cPos == 0) gPos = 180;
if (cPos == 180) gPos = 0;
if (cPos != gPos && millis() >= myservo_movetime) {
moveServo();
}
if (Serial.available() > 0) { GetCommand(); }
}
void moveServo() {
if (cPos < gPos) myservo.write(cPos+1);
if (cPos > gPos) myservo.write(cPos-1);
//if (cPos == gPos) // nothing
myservo_movetime = millis() + tDelay;
myservo_speed = millis();
}
void GetCommand() {
int command = Serial.read() - '0';
int mVal = command;
if (mVal == 'x') {
tDelay = tDelay * 10;
} else {
tDelay = mVal;
}
Serial.print("Pauses changed to : "); Serial.print(tDelay); Serial.println(" mSeconds");
Serial.flush();
}
How can I combine this code to make it work together from one arduino?
Thank you!