Hi All,
Take 2 at a post - the last one crashed on upload.
I'm teaching mechatronics to a class, and am using a simple autonomous robot as one of the introductory learning activities, as per the hundreds of online tutorials, eg:
The robot detects obstacles using the ultrasonic sensor and then looks left and right, driving off in the direction of furthest distance in front of it.
I'm a process engineer in a past life, and have refined the code so it works perfectly for this task. However, I'm trying to extend the task for the few students who: "just get it" while the rest are working out the basics.
I want to set up this 'robot' to slow down when it is within 500mm of an obstacle, gradually slowing down to a crawl as it approaches it. I envisaged that this would be a simple map of the PWM speed to distance,
eg: int LHMotorSpeed = map(distance,50,1,255,60);
I am working ahead of the class and have encountered a programming flaw I can't see that has stopped this from working.
All help gratefully appreciated.
Facts in dot point below.
*jumpers removed off enA and enB pins on the L298N bridge
*Connections, enA=9, enB=10 (PWM-enabled pins)
*in1, in2, in3, in4 to pins 4-7 respectively
*motors turn if analogWrite(enX,255) used in code
*motors do not turn if speed values of 254 or below used in code.
-
code downloaded from the internet to check operation of L298 board works perfectly - connections fine
(Site:https://tronixlabs.com.au/news/tutorial-l298n-dual-motor-controller-module-2a-and-arduino/) -
have tried the following:
- directly forcing datatype to int through: int MotorSpeed = map(...). Did not work
- casting datatype: MotorSpeed = (int) map... Did not work
- creating a new int variable and assigning the value to the new variable. Did not work
- typing in "254" at the same spot where "255" allowed the motors to turn. Motors did not turn
- giving up in frustration and looking at it with a new pair of eyes/objectively.
It's almost as though my code is assigning a boolean value of on or off, but I couldn't see how.
- Test code works and appears identical.
Could anyone comment on possible problems here please - I'm at a wits end with this one, and have exhausted the options I could think of.
Code that works = L298_Motor_Test_PWM
My code that doesn't = Autonomous_Cr_final_9_Jul
Autonomous_Cr_final_9_Jul.ino (5.05 KB)
L298_Motor_Test_PWM.ino (1.94 KB)