Hello,
So this is the first time using the forum. I think I have this in the right topic area, but if not, sincere apologies. So the problem: for a project I am to set up my Uno to control DC motor speed using a potentiometer, while simultaneously controlling servo sweep from 0-180 degrees and back. The project is to use this setup to basically make an oscillating fan-Mechanical Engineering student, first year, in case anyone is wondering. We get extra credit for having "extras", i.g. for mine I have a red LED for when the motor is off, a green LED for when it is on, and will put a blue LED that fades with output from the potentiometer. Anyway, my code works, kind of. The DC motor speed is adjustable and the Red/Green LED come on as expected when its just the code for the motor, but when I add in the code for sweep control, the DC motor and LEDs only change after the servo has done its 180 sweep and back. Its super bizarre and I've been searching threads/scratching my head to figure it out. If anyone has any insight, please share it. I am using the Sparkfun Inventors Kit for all my hardware. My code is as follows:
int analogInPin = A0;
int sensorValue = 0;
int outputValue = 0;
int DC_pin = 11;
#include <Servo.h>
Servo myservo;
int pos=0;
void setup(){
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
pinMode(DC_pin, OUTPUT);
myservo.attach(2);
}
void loop() {
sensorValue = analogRead(analogInPin)/4;
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(DC_pin, sensorValue);
if (sensorValue >= 5)
{
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
}
else
{
digitalWrite(5, HIGH);
digitalWrite(7, LOW);
}
myservo.attach(2);
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
I know it is a little messy, I am still brand new to coding and learning all the ins and outs. Again, thank everyone so much for any help! I appreciate it all.