By continuing to use this site, you agree to our use of cookies. Find out more

Arduino Software Help Using an EDF

All Topics | Latest Posts

Search for:  in Thread Title in  
Tom Hicks14/03/2017 16:26:23
19 forum posts

Hello everyone,

I am unsure whether i am in the right place asking for help like this but i figured it's worth a shot with such a large community!

I am very new to the world of EDF's, ESC's, etc. and i am trying to produce arduino software so when a button is pressed the EDF runs at max speed and when the button is pressed again the EDF stops.

I managed to find some code online which helped me get the fan up and running in a particular cycle but when i modified this code for my application it isn't working how i would like it to.

Right now my code starts the motor when the button is pressed, runs at full speed for one second and repeats, pressing the button again does nothing...

I will post the code below and look forward to hearing your advice, or if someone could point me in the right direction it would be much appreciated.

Thank you for your time!

CODE:

#include <Servo.h>
const int sensorPin = A0;
int switchState = 7;
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()
{
switchState = analogRead(sensorPin);

{
int speed;

if (switchState == LOW) {

digitalWrite(9, LOW);
}

else (switchState == HIGH); {

digitalWrite(9, HIGH);

Serial.println("On";
for(speed = 0; speed <= 100; speed += 1) {
setSpeed(speed);
Serial.println(speed);
delay (100);

}
}
}
}

Bob Burton14/03/2017 18:51:54
186 forum posts

Tom

I will take a look but the forum software has turned some of your code into smilies but I should be able to determine what was really there.

Bob Burton14/03/2017 19:00:39
186 forum posts

Ok. I can see what is wrong and I will try to find time to fix it later this evening, but no promises, so it may be tomorrow.

What is connected to pin 9 ?

Tom Hicks14/03/2017 19:03:14
19 forum posts

Thanks Bob, I have many different versions all of which are failed attempts! I appreciate you taking a look for me. I will alter the code in this post so it should remove smilies!

#include <Servo.h>
const int sensorPin = A0;
int switchState = 7;
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()
{
switchState = analogRead(sensorPin);

{
int speed;

if (switchState == LOW) {

digitalWrite(9, LOW);
}

else (switchState == HIGH); {

digitalWrite(9, HIGH);

Serial.println("On" ;
for(speed = 0; speed <= 100; speed += 1) {
setSpeed(speed);
Serial.println(speed);
delay (100);

}
}
}
}

Tom Hicks14/03/2017 19:04:19
19 forum posts

Hmmm.... for some reason it keep changing it back, even when a space is added! The wink smiley is nothing but a close bracket! how strange!

Bob Burton14/03/2017 19:10:47
186 forum posts

Belay the question about pin 9 !

Tom Hicks14/03/2017 19:15:02
19 forum posts
I look forward to seeing it and finding out what I've done wrong.

Pin 9 is the signal wire going to the ESC.
Max Z14/03/2017 19:34:08
avatar
565 forum posts
248 photos

To begin with, you are mixing up datatypes. You specify switchState as an integer, you do an analogRead of the sensor pin and later you use it in a comparison expecting it to behave as a boolean (==HIGH, ==LOW)

Secondly, why are you doing a digitalWrite(9)? Increasing speed in the for loop and calling setSpeed should be enough, since myServo is already attached to pin 9, and will be updated in setSpeed.

Thirdly, the comparison in else is not necessary, If switchState is not LOW it will be HIGH ("else" should be "else if" if you are using a comparison anyway).

Change the main loop to if ......==HIGH; <insert your "for" sequence>; else speed = 0;

Hope this helps you,

Max.

Bob Burton14/03/2017 19:35:40
186 forum posts

The wink smiley is a close bracket followed by a semicolon and that sequence appears in the code, hence the smilies

Phil Green14/03/2017 19:43:53
avatar
1665 forum posts
344 photos

The logic & syntax is all mixed up Tom.
You've defined switchState as an integer, preset to 7 (why? is this the button input on D7?)
in setSpeed() you do switchState=analogRead(A0) which overwrites the '7' and gives switchState a value from 0 to 1023 from the ADC.
You then ask if switchState is LOW which is a binary function, presumably you wanted to test a button here?
theres no 'if' accompanying the 'else' following the second switchState test for equal to HIGH, so the compiler treats that as an assignment rather than the intended test.
You dont seem to do anything with the actual ADC reading, I think you intended assigning it to a separate variable but instead re-used switchState, which elsewhere seems to be the button, high or low.

The logic then ramps up the speed to 100 over a period of 10 seconds (100ms x 100) whilst pin 9 simply follows pin 7 and otherwise has no effect on proceedings.  

Can you explain better what you want to do please, and how the hardware is connected?

Cheers
Phil

 

 

 

Edited By Phil Green on 14/03/2017 19:59:36

Bob Burton14/03/2017 19:47:54
186 forum posts

OK. Give this a go

#include <Servo.h>
const byte buttonPin = A0;
byte currentSwitchState;
byte previousSwitchState;
Servo theESC;
boolean escOn = false;

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

void setup()
{
Serial.begin(115200);
theESC.attach(9);
pinMode(buttonPin, INPUT_PULLUP); //turn on built in pullup resistor
arm();
}

void loop()
{
previousSwitchState = currentSwitchState;
delay(100); //crude debounce
currentSwitchState = digitalRead(buttonPin);
if (currentSwitchState != previousSwitchState) //button state has changed
{
if (currentSwitchState == LOW) //button is pressed
{
escOn = !escOn;
if (escOn)
{
Serial.println("ON";
}
else
{
Serial.println("OFF";
}
}

if (escOn)
{
theESC.write(180); //full speed
}
}
else
{
theESC.write(0); //stop
}
}

Remember, the smilies are a closing bracket and a semicolon.

NOTE:

I used the same arming sequence as the original code although I removed the setSpeed function
The button needs to be wired to take pin A0 to GND when pressed
The code activates the built in pullup resistor to keep the input in a known state
Be careful when you run this with a motor attached as the logic may be reversed so expect the unexpected

I don't have an ESC and motor or a servo handy to test the code, but the output to the Serial monitor produces the expected ON/OFF messages

Let me know how it performs, or if it doesn't !

Max Z14/03/2017 19:51:47
avatar
565 forum posts
248 photos
Posted by Phil Green on 14/03/2017 19:43:53:


in setSpeed() you do switchState=analogRead() which overwrites the '7' and gives switchState a value from 0 to 1023 from the ADC.

?? I don't see that, Phil.

Phil Green14/03/2017 19:55:06
avatar
1665 forum posts
344 photos

Which bit Max?   switchState is an int, preassigned with 7.
Then in loop() Tom says: switchState = analogRead(sensorPin);
If A0 has a pullup and a button to ground, switchState will then hold zero or 1023 (but not reliably with ADC noise)

Buttons should be read with digital inputs, ADCs are always susceptible to +/- a bit of quantisation.

Edited By Phil Green on 14/03/2017 20:05:31

Tom Hicks14/03/2017 19:59:55
19 forum posts

Cannot believe the input I have received from you guys.

I forgot to mention that i am also relatively new to Arduino IDE, i touched on it briefly last year but since then I have been using Matlab and Simulink for my Arduino. I have no motor on me at the present either Bob, but i cannot wait to try it out and feedback to you tomorrow. From what i can see it is also working well in the serial monitor.

I have used snippets from the source i found online and also from the starter manual that comes with the Arduino Uno but i have quickly figured that this is a lot more complex than what i first thought!

Thanks again everyone, I would probably have never noticed my mistakes.

Once again, i will report tomorrow on the outcome of the code provided from Bob.

Max Z14/03/2017 20:20:00
avatar
565 forum posts
248 photos

@Phil: your original post referred to the setSpeed routine, I did not find it there. Your edited post is consistent with my comments.smiley

@Bob: You did not include the run up sequence that Tom has included (but forgot to mention in the description). But you did include the pushbutton toggle which I ignored sad

Max.

Bob Burton14/03/2017 20:45:34
186 forum posts

I deliberately took out the run up sequence to keep things simple to start with. Once the toggle on/toggle off is working then features can be added back if needed.

Tom's original requirement was

" when a button is pressed the EDF runs at max speed and when the button is pressed again the EDF stops."

No mention of a slow start so I don't know whether he wants that or not. Easy enough to add if required.

Tom Hicks15/03/2017 09:55:51
19 forum posts
Morning.

I have tested the code which Bob provided to me yesterday evening and unfortunately have came across an issue.

Once the button is pressed the fan starts and runs, once the button is pressed again it runs even faster. Once the button is pressed a third time the fan switches off (serial monitor says "ON" however). Any further button presses don't do anything now and the fan makes a strange beeping.
I have recorded this issue which can be seen here: https://youtu.be/JYEw9p-NvrM

Are there any reasons why this is happening?

Please note, the fan and ESC in the video is not the one which I will be using as a final project, it's just being used to get the software sorted.

Thanks guys!
Tom Hicks15/03/2017 09:56:08
19 forum posts
Morning.

I have tested the code which Bob provided to me yesterday evening and unfortunately have came across an issue.

Once the button is pressed the fan starts and runs, once the button is pressed again it runs even faster. Once the button is pressed a third time the fan switches off (serial monitor says "ON" however). Any further button presses don't do anything now and the fan makes a strange beeping.
I have recorded this issue which can be seen here: https://youtu.be/JYEw9p-NvrM

Are there any reasons why this is happening?

Please note, the fan and ESC in the video is not the one which I will be using as a final project, it's just being used to get the software sorted.

Thanks guys!
Tom Hicks15/03/2017 09:56:14
19 forum posts
Morning.

I have tested the code which Bob provided to me yesterday evening and unfortunately have came across an issue.

Once the button is pressed the fan starts and runs, once the button is pressed again it runs even faster. Once the button is pressed a third time the fan switches off (serial monitor says "ON" however). Any further button presses don't do anything now and the fan makes a strange beeping.
I have recorded this issue which can be seen here: https://youtu.be/JYEw9p-NvrM

Are there any reasons why this is happening?

Please note, the fan and ESC in the video is not the one which I will be using as a final project, it's just being used to get the software sorted.

Thanks guys!
Tom Hicks15/03/2017 09:56:15
19 forum posts
Morning.

I have tested the code which Bob provided to me yesterday evening and unfortunately have came across an issue.

Once the button is pressed the fan starts and runs, once the button is pressed again it runs even faster. Once the button is pressed a third time the fan switches off (serial monitor says "ON" however). Any further button presses don't do anything now and the fan makes a strange beeping.
I have recorded this issue which can be seen here: https://youtu.be/JYEw9p-NvrM

Are there any reasons why this is happening?

Please note, the fan and ESC in the video is not the one which I will be using as a final project, it's just being used to get the software sorted.

Thanks guys!

All Topics | Latest Posts

Please login to post a reply.

Latest Forum Posts
Support Our Partners
CML
electricwingman 2017
Slec
Sussex Model Centre
Subscribe now
Advertise With Us
Sarik
Latest "For Sale" Ads
NEW POLL - has the pandemic altered your event safety perceptions?
Q: Has the covid pandemic deterred you from attending shows and events in 2021?

 No, I'll be attending just as many as I usually do
 No, but I'll choose my event with greater care
 Yes, I'll attend fewer events going forward
 Yes, I wont attend any where previously I have

Latest Reviews
Digital Back Issues

RCM&E Digital Back Issues

Contact us

Contact us

Magazine Locator

Want the latest issue of RCM&E? Use our magazine locator link to find your nearest stockist!

Find RCM&E!