Hello All,
I’m new to this forum and I’m having some trouble with a simple servo twitching spastically ONLY when the other motor runs.
This is a desktop fan build for a school project, and it should follow the basic functions.
- oscillate (servo)
- Fan speed (motor)
- On/Off functions (button 1)
- Oscillate Pause (button 2)
- Safety Feature (Photo-Resistor)
- The photo resistor is mounted below the fan motor, such that as a hand attempts to touch the spinning fan blade, the difference in light causes the photo resistor to change its resistance, that is interpreted and converted to voltage by the program, and then turns the motor and servo off until the hand is removed ( light rises above minimum threshold).
The fan will oscillate when button 2 state is changed, and turns on/off when its supposed to.
The safety feature functions very well.
The motor speed control works.
The servo that controls the oscillate function twitches spastically when the fan motor is running, the rate that is twitches at changes when the fan mode (speed) is changed. The servo as ignores the pwm signal that is being sent to it while the fan is running. (verified by multimeter).
I am using an external 12v 500mAmp power supply to power the board, and voltage between the power and ground on the breadboard drops from 5v to 4.88v when the fan motor is running.
I am at a complete loss at this point and any input would be much appreciated.
Ive attached a picture of the schematic i’m using and the code is listed below.
Also, The servo PWM is not using the same PWM pin that is in the schematic. I accidentally wrote that both the motor and servo were using pin 3 for pwm output when the servo is in fact using pin 9, or pin 5 depending on which version of code i am using. Both respond with the same function.
"
#include Servo.h
Servo oscillate;
int servo_pin = 9;
int pos = 0;
int button_2_pin = 4;
int va1;
int va2;
int button_2_state;
int servo_mode = 0;
int store_servo_mode;
int led_4_pin = 13;
int x = 0;
int x1 = 0;
int y1 = 0;
int button_1_pin = 2; // On/Off button
int led_1_pin = 7; // Mode 1 indicator
int led_2_pin = 8; // Mode 2 indicator
int led_3_pin = 12; // Mode 3 indicator
int fan_speed_output = 3; // PWM for fan speed output
int photo = A0; // Analog pin 0 used for photoresistor
int va3, va4; // Variables used to debouce button state
int button_1_state; // Variable used to store button state
int fan_mode = 0; // In bianary 0 is off and 1 is on.
int reading; // Variable to store photoresist reading
int store = 0; // VARIABLE USED TO STORE CURRENT BUTTON STATE FOR SAFETY FEATURE
float voltage;
void setup(){
pinMode(button_2_pin, INPUT);
pinMode(led_4_pin, OUTPUT);
oscillate.attach(servo_pin);
pinMode(button_1_pin, INPUT);
pinMode(photo, INPUT);
pinMode(fan_speed_output, OUTPUT);
pinMode(led_1_pin, OUTPUT);
pinMode(led_2_pin, OUTPUT);
pinMode(led_3_pin, OUTPUT);
Serial.begin(9600);
button_1_state = digitalRead(button_1_pin); // Set button state to initial reading
button_2_state = digitalRead(button_2_pin);
}
void loop(){
if (x<180){
x = x +1; }
if (x == 180){
x1 = x;
x = 0;}
if (x1<=180){
x1 = x1-1;}
if (x1 == 0){
x = x1;}
if (x == 0){
y1 = 1;}
if (x1 == 179){
y1 = 0;}
reading = analogRead(photo); // Read the value of the photoresistor
voltage = reading*(5.0/1023.0); // Convert value to voltage
va1 = digitalRead(button_2_pin);
delay(10);
va2 = digitalRead(button_2_pin);
if (va1 == va2){
if (va1 != button_2_state){
if (va1 == LOW){
if (servo_mode == 0){
servo_mode = 1;
store_servo_mode = 1;
} else {
if (servo_mode == 1){
servo_mode = 0;
store_servo_mode = 0;
}
}
}
}
button_2_state = va1;
}
va3 = digitalRead(button_1_pin); // Read button 1 state
delay(10); // Delay for debouncing
va4 = digitalRead(button_1_pin); // Read button 1 state
if (va3 == va4){ // IFF both button states ==
if (va3 != button_1_state) { // IFF there is a change in button state
if (va3 == LOW){ // IFF button state is off
if (fan_mode ==0) { // IFF fan is off
fan_mode = 1; // Change Mode to 1
store = 1; // STORE THE CURRENT STATE FOR USE WITH SAFETY FEATURE
} else {
if (fan_mode == 1) { // IFF the mode is 1 and there is change in button state
fan_mode = 2; // Change Mode to 2
store = 2; // STORE THE CURRENT STATE FOR USE WITH SAFETY FEATURE
} else {
if (fan_mode ==2) { // IFF the mode is 2 and there is a change in button state
fan_mode =3; // Change the Mode to 3
store = 3; // STORE THE CURRENT STATE FOR USE WITH SAFETY FEATURE
} else {
if (fan_mode =3) { // IFF the Mode is 3 and there is a change in button state
fan_mode = 0; // Change the Mode to 0
store = 0; // STORE THE CURRENT STATE FOR USE WITH SAFETY FEATURE
}
}
}
}
}
button_1_state = va3; // Store button state for next loop cycle
}
}
if (voltage < 3.0){ // IFF photoresist voltage reading is less than 3.0v
servo_mode = 0; // TURN THE SERVO OFF!!!!
fan_mode = 0; // TURN THE FAN OFF!!!!!
} else { // IFF photoresist voltage is more than 3.0v
fan_mode = store; // TURN THE FAN BACK ON!!! IN THE CURRENT MODE!!!
servo_mode = store_servo_mode; // TURN THE SERVO BACK ON!!!
}
if (servo_mode ==1){ // IF Mode is 1
if (y1 == 1){ // IF servo loop is "ascending"
pos = x; // Servo Position is = ascending value
oscillate.write(pos); // TURN THE SERVO!!
delay(15); // WAIT FOR THE SERVO TO STOP!!!
digitalWrite(led_4_pin, HIGH); } // TURN ON THE INDICATOR LIGHT
if (y1 == 0){ // IF SERVO LOOP IS "DESCENDING"
pos = abs(x1); // SERVO POSITION = DESCENDING VALUE
oscillate.write(pos); // TURN THE SERVO!!
delay(15); // WAIT FOR THE SERVO TO STOP!!
digitalWrite(led_4_pin, HIGH); } // TURN ON THE INDICATOR LIGHT
}
if (servo_mode ==0){ // IF MODE IS 0
digitalWrite(led_4_pin, LOW); } //DO NOTHING!!!
if (fan_mode == 0){ // IF Mode is 0
analogWrite(fan_speed_output, 0); // DO NOTHING!!!!
digitalWrite(led_1_pin, LOW);
digitalWrite(led_2_pin, LOW);
digitalWrite(led_3_pin, LOW);
}
if (fan_mode == 1){ // IF Mode is 1
analogWrite(fan_speed_output, 85); // FAN SPEED LOW, 1 LED ON
digitalWrite(led_1_pin, HIGH);
digitalWrite(led_2_pin, LOW);
digitalWrite(led_3_pin, LOW);
}
if (fan_mode == 2){ // IF Mode is 2
analogWrite(fan_speed_output, 170); // FAN SPEED MEDIUM, 2 LED's ON
digitalWrite(led_1_pin, HIGH);
digitalWrite(led_2_pin, HIGH);
digitalWrite(led_3_pin, LOW);
}
if (fan_mode == 3){ // IF Mode is 3
analogWrite(fan_speed_output, 255); // FAN SPEED HIGH, 3 LED's ON
digitalWrite(led_1_pin, HIGH);
digitalWrite(led_2_pin, HIGH);
digitalWrite(led_3_pin, HIGH);
}
delay(0);
}