I have a weird problem. again. This time it's the PWM. I have used PWM before and I used to work the way I am doing it now. But now it just wont work. The Fading
sketch from example works. When I compile a code with the similar code it doesn't work. Everything with respect to compilation is same.
The code that works:
int ledPin = 3; // LED connected to digital pin 9
void setup() {
// nothing happens in setup
}
void loop() {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
}
The code that doesn't work.
#define FAN1 3
#define FAN2 5
void setup() {
//EMPTY
}
void loop() {
int speed1 = 25;
int speed2 = 127;
analogWrite(5, speed1);
analogWrite(3, speed2);
delay(10000);
}
Why doesn't the second code work?
EDIT 1:
By not working I meant No output on Second Code and Expected Output on First one. Currently I'm using ISIS
for simulation and Oscilloscope
to view the output along with a L239D
with motors attached.
In the first code the motor starts and stops as expected, with the expected PWM Waveform.
In second code, nothing. No waveform, no motor movement.
-
2Could you clarify what you mean by "doesn't work", please? What does happen, and how are you determining this?Mark Smith– Mark Smith01/20/2017 10:28:46Commented Jan 20, 2017 at 10:28
-
@MarkSmith edited the question. Thanks for pointing out the lacking.echo_salik– echo_salik01/20/2017 10:44:07Commented Jan 20, 2017 at 10:44
1 Answer 1
One omission from your code which might be causing the problems is the fact that you haven't set the pinMode. You need to set the pins as OUTPUT in setup()
#efine FAN1 3
#define FAN2 5
void setup() {
pinMode(FAN1, OUTPUT);
pinMode(FAN2, OUTPUT);
}
void loop() {
int speed1 = 127;
int speed2 = 25;
analogWrite(FAN1, speed1);
analogWrite(FAN2, speed2);
delay(10000);
}
I am curious to know why you declared FAN1 and FAN2, but didn't use them - I'd recommend changing the analogWrite statements to use them instead of referring to the pin numbers directly.
Also, the way you're using the pin and speed means that you're using speed2 for FAN1 and vice versa, I've switched these in my example so FAN1 and speed1 go together.
-
Yeah. I meant to use it but didn't to check it out if it was to blame. I was just checking. Let me post a result of this change, btw the code that does work also doesn't set the pins as outputs.echo_salik– echo_salik01/20/2017 11:52:33Commented Jan 20, 2017 at 11:52
-
just checked it. still the same.echo_salik– echo_salik01/20/2017 11:55:38Commented Jan 20, 2017 at 11:55
-
1I think there is something wrong with the Arduino Library of
ISIS
. I just got back my arduino back from a friend an it works.echo_salik– echo_salik01/20/2017 11:59:37Commented Jan 20, 2017 at 11:59 -
You don't need pinMode with analogWrite. Check the first couple of lines of the source for analogWrite to see why. It takes care of pinMode itself.Delta_G– Delta_G06/04/2017 01:40:49Commented Jun 4, 2017 at 1:40