New answers tagged

0

What's happening is that you've written a fraction in a way that the compiler treats it as integer math. C/C++ truncate fractions, so 255/100 is just 2 (and if any of the rest of those are integers, they cause truncation too). What you want to do is make some number in that calculation a decimal--it doesn't matter which one--or cast an input to a float with (...


3

You need to review your code and also the capabilities of the Arduino UNO. int declares an integer. So int duty = 0.5; is going to get rounded to either 0 1. delay(0); also will not work. The instruction will simply get skipped. Likely the reason why you get a brighter LED. Try to use delayMicroseconds(); if you need shorter time but the minimum delay ...


0

The solution is, You need to implement custome pulse in funtion unsigned long rdPulseIn(int pin, int value, int timeout) { // the following comments assume that we're passing HIGH as value. timeout is in milliseconds unsigned long now = micros(); while(digitalRead(pin) == value) { // wait if pin is already HIGH when the function is called, but ...


0

Modifying the library code file L298N.cpp. Insert 2 lines in setSpeed(): void L298N::setSpeed(unsigned short pwmVal) { _pwmVal = pwmVal; if (_ismoving == true) // +++ analogWrite(_pinEnable, _pwmVal); // +++ } Problem solved!


0

Try different pins than 5 and 6, because those PWM generators share the timer with delay(), which I'd interpret as: You use analogWrite() to set the timer to fire every n microseconds to toggle your PWM signal -- and then you call delay(1000), which sets that same timer to fire in one second to continue the program. Pins 10 and 11 do use a different timer, ...


1

The previous responses already showed the main issue with the program. Here I am providing a few suggestions that hopefully could still improve it: Instead of using delay(), you could manage the time with millis(). This will make the ramp smoother, and you will be able to add code to the loop if needed that will not be blocked by the delays. Storing the ...


1

You are trying to smoothly ramp up the PWM value from 21% to 65% over a period of 30 seconds at 1 second intervals, which does not work very well because the PWM value has to be a byte. That means that the value written into the PWM register changes from 21*255/100 to 65*255/100, which is 53.55 to 165.75. Use values of 53 and 166, which gives a ...


3

float pwmDelta=((255/100)*(highLim-lowLim))/(tLim); That line should be: float pwmDelta=((255.0/100)*(highLim-lowLim))/(tLim); Adding the .0 tells the compiler to do that math using floats and you get 2.55. Without it the math is done with int and the result is 1 which isn't what you want. analogWrite(motor1, 53.55); Just write 54. analogWrite only ...


1

The if condition is causing the issue. When the global variable 'fade' reaches 240, upon entering the loop(), you are incrementing it by 15, making 'fade' = 245. So, the if condition becomes true and 15 is subtracted from 245 making 'fade' to be equal to 240 again. This keeps repeating and hence the value 'fade' variable is stuck between the values 240 and ...


4

The problem is that you add 15, and than if it's >= 240, you decrease 15, so it will not change (I wouldn't consider it haywire though). You can better use a direction sign. Below is only the relevant code (not the GPIO functionality). What it does in the loop is, in case up is true, it adds fifteen, otherwise it subtracts 15. When 255 is reached, it ...


Top 50 recent answers are included