Notifications
Clear all

problem interfacing speed sensor lm393 using car robot moved by RF remote

15 Posts
3 Users
1 Reactions
957 Views
(@habby)
Member
Joined: 2 years ago
Posts: 7
Topic starter  

hello every one i was just trying to build robot car that can be moved by RF remote  so that i can monitor its speed using LM393 speed sensor through lcd.

the problem come after uploading the below code is only remote is detected but the motor while the whole system seems to freez means the all motors not moving but when i try to drive the car without speed sensors its moving any one to help me?

#

include <Wire.h>
#include "TimerOne.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16,2);

#include <IRremote.h>
#define Next_button 765
#define Prev_button 8925
#define left_button 43095
#define right_button 57375
#define Stop_button 49725
int receiver_pin = 8;
//initializing the pins for leds
int left_motor1 = 4;
int left_motor2 = 6;
int right_motor1 =5;
int right_motor2 = 7;
int motorEnA = 9;
int motorEnB = 10;
IRrecv receiver(receiver_pin);
decode_results output;
int encoder = 2;
volatile unsigned int counter;
const byte MOTOR_A = 3;
const byte MOTOR_B = 2;

const float stepcount = 20.00;
const float wheeldiameter = 66.10;
float diskslots = 20;
volatile int counter_A = 0;
volatile int counter_B = 0;

void ISR_countA()
{
counter_A++;
}

void ISR_countB()
{
counter_B++;
}
int CMtoSteps(float cm) {
int result;
float circumference = (wheeldiameter * 3.14) / 10;
float cm_step = circumference / stepcount;

float f_result = cm / cm_step;
result = (int) f_result;

return result;
}
void ISR_timerone()
{
Timer1.detachInterrupt();
float rotation1 = (counter_A / diskslots) * 60.00;
counter_A = 0;
float rotation2 = (counter_B / diskslots) * 60.00;
counter_B = 0;
Timer1.attachInterrupt( ISR_timerone );

}


void setup()
{
Timer1.initialize(1000000);
attachInterrupt(digitalPinToInterrupt (MOTOR_A), ISR_countA, RISING);
attachInterrupt(digitalPinToInterrupt (MOTOR_B), ISR_countB, RISING);
Timer1.attachInterrupt( ISR_timerone );
receiver.enableIRIn();
lcd.begin();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("car speed");
lcd.setCursor(4,1);
lcd.print("limiter");
pinMode(left_motor1, OUTPUT);
pinMode(left_motor2, OUTPUT);
pinMode(right_motor1, OUTPUT);
pinMode(right_motor2, OUTPUT);
pinMode(motorEnA, OUTPUT);
pinMode(motorEnB, OUTPUT);
}
void countpulse()
{
counter++;
}

void loop() {

if (receiver.decode(&output))
{
unsigned int value = output.value;
switch(value) {
case right_button:
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,HIGH);
digitalWrite(right_motor2,LOW);
analogWrite( motorEnA,130);
analogWrite( motorEnB,130);
break;
case Prev_button:
analogWrite( motorEnA,250);
analogWrite( motorEnB,250);
digitalWrite(left_motor1,HIGH);
digitalWrite(left_motor2,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,HIGH);
break;
case Next_button:
analogWrite( motorEnA,250);
analogWrite( motorEnB,250);
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,HIGH);
digitalWrite(right_motor2,LOW);
break;
case left_button :
digitalWrite(left_motor1,HIGH);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,HIGH);
break;
case Stop_button:
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,LOW);
break;
}
receiver.resume();

}
}

 


   
Quote
(@davee)
Member
Joined: 3 years ago
Posts: 1794
 

Hi @habby,

  Fixing problems with interrupts is always tricky, as you can't be sure which bit of 'main code' will get interrupted. So you have to play by 'stricter' rules than with purely polled software.

I haven't played with interrupts on Arduino, so apologies if this is a red herring, but the calls to

Timer1.detachInterrupt(); and Timer1.attachInterrupt(); 

in an ISR do not feel right. 

If you are trying to prevent an interrupt during your ISR, they are unnecessary ... I would expect interrupts to be disabled. (maybe https://gammon.com.au/interrupts is helpful?) Also, there appear to be interrupt enable/disable functions.

Of course, this may have nothing to do with your problem ... maybe others with more relevant experience can recognise your problem, or correct any misunderstanding on my part.

Best wishes, Dave


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@davee @habby I copied your code then I used Tools -> Auto Format to make it somewhat readable. I found several parts of the code are actually never referenced.

I have worked with interrupts and timers but mostly on the ESP32 MCU although the concepts are the same and the library this is based on has what I will kindly call 'strangeness' in it. 

@davee is 100% correct in his observations but didn't go far enough. It appears you have used a library of questionable utility and maybe copied code from separate sketches but they use two different kinds of timers.

You need to point us to the documentation you referenced in order to write your code.

The timer interrupt is badly designed. The normal approach is to simply set a boolean flag in the ISR, then in the main loop detect the flag set and do the heavy lifting. HOWEVER, I see no way for that interrupt to fire. Even if you didn't know that technique, you also did not put the code in fast ram and you didn't mark the code as critical section. That means the compiler did not produce very optimized code for an ISR and it will very likely get interrupted itself. 

I am not C++ trained and never used it professionally, only played a tiny bit as a hobbyist several decades ago. I can't read C++ very well, but I do notice some very strange code in the library. It looks similar to code I saw before that is extremely low level all the way down to the hardware. I have written several interrupt driven and timer interrupt driven sketches and only ever used the standard arduino API. The code you are using is undoubtedly a few microseconds faster but Rule 1 applies (know what you are doing rule)

Sorry to be so blunt, but these are my hunches which is all I have to go on since you have not told us where you got the code from.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@habby)
Member
Joined: 2 years ago
Posts: 7
Topic starter  

@zander Thanks for replying the code on how to deal with speed sensors I copied it at this link https://dronebotworkshop.com/robot-car-with-speed-sensors but for the case of working with remote I just write it myself with the help of RF remote liblary examples that found in the liblary folder of Arduino ide 

 

 

 


   
ReplyQuote
(@habby)
Member
Joined: 2 years ago
Posts: 7
Topic starter  

@davee Thanks for your reply 


   
DaveE reacted
ReplyQuote
(@habby)
Member
Joined: 2 years ago
Posts: 7
Topic starter  

@zander but even my self I know that the problem is that this two liblary use different timer and I have been struggling to fix it so that they can use the same timer but I stuck here 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@habby Ok, but you might want to check your code, I found several parts that are never referenced indicating you may have either forgot or misunderstood something.

countpulse is never referenced

CMtoSteps is never referenced

counter is never referenced because it's only used in countpulse

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@habby)
Member
Joined: 2 years ago
Posts: 7
Topic starter  

@zander ok thanks Im working on it and I will give you feedback 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@habby They can't and shouldn't use the same timer. The working sketch you copied from Bill uses two timers, one is the hardware pin interrupt, and the other is the hardware timer interrupt. The hardware timer is set for 1 sec. On a modern MCU 1 sec is a very long time, a lot of code can execute so the long ISR timer code is not a problem since Arduino made the Serial library so much faster.

Now that you tell me your sketch is based on Bills sketch and you simply want to add IR control I expect to see code that looks like that. I don't think I do. What I expect to see is Bill's code exactly as before and then you add some code (maybe stick a comment on the end of the added line such as // added for IR remote on every line you add other than the main loop which is all yours. That might help others to focus on only your changes from Bill's working code. If you can do that, then we can probably get your problem resolved fairly quickly. I will download Bill's code and start to do as I suggested to see if something pops up quickly.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@habby)
Member
Joined: 2 years ago
Posts: 7
Topic starter  

@zander okay  I understand let me post the code again


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@habby If I may suggest, put all the IR includes, variable declarations in a commented block near the beginning. do similar for setup. I would think about using Bill's move code as is and just put the IR decode logic in the loop. I always attack problems in pieces so step one is simply repeat what Bill has. What I mean is in his Setup function he has a Test Motor Movement block. Remove that and place in the loop, then preface each step of the test with your IR decode code. you shouldn't need to write new move left motor code as Bill has it already.

Am I being clear?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@habby Here is another idea. First do not use #define use const int. Second change the names below to match the sub-routines as in Forward, Reverse, Spin Left, etc. I have no idea what Prev and Next mean. Use proper names, NOT Next, Prev

#define Next_button 765
#define Prev_button 8925
#define left_button 43095
#define right_button 57375
#define Stop_button 49725

 

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@habby Slowly I am understanding. Bill has two sketches, the first is simply demonstrating the use of hardware interrupts to measure speed. This means you have 3 different sketches to be merged and one is only in your head. Here is my suggestion. Bills first sketch is sketch 1 etc yours is 3)

1. Get the second of Bills sketches working.

2. Add whatever code is necessary for IR control (group together and comment where possible) AND your case logic becomes a call to Bill's routines MoveForward etc. I realize there may not be a one to one match but leave that for later.

3. Add the sketch 1 code to display on Serial the motor stats in either setup or conditionally in loop under either a millis control or a IR button.

4. Now add the code for other movements modelled on what is already there.

5. Now convert the Serial output to LCD? I mention that as you have included that library.

6. Done?

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@habby)
Member
Joined: 2 years ago
Posts: 7
Topic starter  

@zander I  have already do that and the motor is working perfect but when I introduce speed sensor the controller seems to stop everything 

But right now Im in consideration of working with interrupt  because it is the source of problem 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@habby Remember, the speed sensor interrupt is only used for demonstration purposes in Bill's sketch one. You can totally ignore that for now. You should be using Bill's sketch two as your base to build on, not sketch one. See my longer answer I did a few minutes ago, I would leave his sketch one unique content which is just serial output of some speed sensor data to later in the development cycle. Go slow, change ONE thing at a time and take small steps.

BTW, what are the extra two motors that you analogwrite to????

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote