I'm working on a fan project for my engineering class, I have to make a button turn on a dc motor and servo motor and have a potentiometer control the speed of my dc motor.
I have it figured out mostly but I keep running into some problems that I've spent hours working on and I still can't seem to figure out:
*The dc motor won't start unless I literally spin it a little with my hand to get it started, I think it's a problem with it getting enough power, but I have it plugged into the 5v power source and it's the only thing besides the potentiometer that's getting powered by it.
*The potentiometer adjusts the speed of the DC Motor but there's a delay between when I move the potentiometer to a different value and when you can observe the difference in speed on the motor.
*The servo motor shakes a little at the end of each rotation and when the button is off, instead of just not moving it continues to just shake until you turn it on again.
*The button turns everything off but the servo motor first finishes it's movement back to the original position, I was wondering if there was anyway to make it just stop where it was, but that's not too important.
I saw someone else had a very similar problems but the way they wrote their code is different from mines, I'm using interrupts and I'm still new to writing code and not sure how to make what worked for them work for me without just completely copying their code.
Any help at all about any of the problems above would be much appreciated.
int button_interrupt = 0;
int toggle_on = false;
int motor = 5;
int sensor_pin = A0;
int servo_pin = 10;
#include<Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600);
attachInterrupt( button_interrupt, handle_click, RISING); //
pinMode(13, OUTPUT);
pinMode (motor, OUTPUT);
myservo.attach(servo_pin);
pinMode (servo_pin, OUTPUT);
}
void loop() {
int pot_val, motor_speed;
pot_val = analogRead( sensor_pin );
motor_speed = map(pot_val, 0, 1023, 0, 255);
if ( toggle_on ) {
digitalWrite(13, HIGH);
Serial.println("on");
analogWrite(motor, motor_speed);
int pos = 0;
int dtwait = 15;
for(pos=0; pos <180; pos +=1){
myservo.write(pos);
delay(dtwait);
}
for (pos = 180; pos>=1; pos-=1){
myservo.write(pos);
delay(dtwait);
}
} else {
digitalWrite(13, LOW);
Serial.println("off");
analogWrite (motor, 0);
digitalWrite(servo_pin, LOW);
}
}
void handle_click()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if ( interrupt_time - last_interrupt_time > 200 ) {
toggle_on = !toggle_on;
}
last_interrupt_time = interrupt_time;
}

