Go Down

Topic: Controlling a ducted fan (Read 701 times) previous topic - next topic

ewokkillah

Hi There,
I'm trying to control a ducted fan with a brushless motor. What I want to have it do is when i press a button have it go full speed. I tryed modifying an existing sketch that sweeps the speed of the motor up and down, but I cant seem to get it to work. so any help or advice would be greatly appreciated.
Thanks
This is a copy of the sketch I'm working from:

#include <Servo.h>

Servo myservo;

void arm(){
// arm the speed controller, modify as necessary for your ESC
Serial.println("arming");
setSpeed(30);
delay(2000);
setSpeed(90);
delay(2000);
Serial.println("armed");
setSpeed(30);
delay(2000);
}

void setSpeed(int speed){
// speed is from 0 to 100 where 0 is off and 100 is maximum speed
//the following maps speed values of 0-100 to angles from 0-180,
int angle = map(speed, 0, 100, 0, 180);
myservo.write(angle); 
}

void setup()
{
Serial.begin(115200);
myservo.attach(9);
arm();
}

void loop()
{
int speed;

Serial.println("Sweeping up");
for(speed = 37; speed <= 90; speed += 1) {
  setSpeed(speed);
  Serial.println(speed);
  delay(100);
}
setSpeed(30);
delay(1000);
Serial.println("Sweeping down");
for(speed = 90; speed > 37; speed -= 1) {
  setSpeed(speed);
  Serial.println(speed);
  delay(100);
}
Serial.println("30 halting...");
setSpeed(30);
delay(5000);
}
|-o-|(-o-)|-o-|

MarkT

[ don't forget those code tags please, # button... ]

So you posted a sketch - is this the working one or the one that's failing?  Did the original
sketch work at all?

How is it failing?

How did you modify the sketch?

Did you record the last change you made before it stopped working?
[ I won't respond to messages, use the forum please ]

ewokkillah

thanks for your reply. This is the original sketch it works to sweep the speed of the motor up and down.
Code: [Select]

#include <Servo.h>

Servo myservo;

int switchPin = 8;

void arm(){
// arm the speed controller, modify as necessary for your ESC
Serial.println("arming");
setSpeed(30);
delay(2000);
setSpeed(90);
delay(2000);
Serial.println("armed");
setSpeed(30);
delay(2000);
}

void setSpeed(int speed){
// speed is from 0 to 100 where 0 is off and 100 is maximum speed
//the following maps speed values of 0-100 to angles from 0-180,
int angle = map(speed, 0, 100, 0, 180);
myservo.write(angle); 
}

void setup()
{
Serial.begin(115200);
myservo.attach(9);
pinMode(switchPin, INPUT);
arm();
}

void loop()
{
int speed;

Serial.println("GO");
if (digitalRead(switchPin) == HIGH);
{
  setSpeed(90);
  Serial.println(speed);
  delay(4000);
}

Serial.println("STOP");
if (digitalRead(switchPin) == LOW);
{
    setSpeed(0);
  Serial.println(speed);
  delay(100);
}
Serial.println("30 halting...");
setSpeed(30);
delay(5000);
}

this is my modification it turns the motor on full speed then off but no functionality of the button.
|-o-|(-o-)|-o-|

Chagrin

Your delay of 100ms is not much of a delay to really see the motor even change speed, much less stop.

Do you have your button wired correctly between the digital pin and GND and an additional 10K resistor between the digital pin and 5V?

ewokkillah

I have a 10k pull down resistor on the ground. I don't really want the speed of the motor to change I want it full speed when the button is pushed. Should I increase the delay?
Thanks
|-o-|(-o-)|-o-|

Go Up