Using MEGA2560 with several interupts and I2C

Hello All,

I'm trying to build a smart car. So I have a frame 4 motors and a Motor Controller board (L293D). Controlling the motors is easy but in order to make the motor drive straight I bought four LM393 speed sensors.
Those will have to be connected to interrupt pins. If I do so I will loose the possibility to use I2C as Motor Controller uses allready Pin 3,5,6 and 11. For the interupts I could use pin 2,18,19 and 20.
Therefore I would like to know if there is any other option to establish a I2C connection? Because I would like to add a couple of sensors and maybe an LCD or OLED screen.

Thanks in advance

Ray

Could you not move the motor controller to 6,7,8 and 11? All appear to be PWM capable if required. This would free up int4 and int5 on pins 2 and 3?

No, polling works fine for those sensors. But if you insist, almost all pins are interrupt pins. Look up pin change interrupts.

Will be hard or I have to wire it instead of placing the shield on top of the MEGA

Those shields are a pain in the butt, and you are much better off avoiding that ancient, extremely inefficient L293D motor driver. Pololu has the best selection of modern, efficient motor drivers.

1 Like

I'm confused I've read quite some articles that to get a good speed reading interrupt pin are needed to reduce the possibility counts are missed.

I want to use these these readings together with PID control so I can set up the speed of the motors accurate and be able to drive straight with the Rover. Today I let them run ut I culd see with my bear eyes that speeds are not equal when assigning the same speed value.

Interrupts generally cause beginners many more problems than they solve, and with proper attention to programming, you won't miss many transitions with polling. Polling is actually faster than interrupt programming.

Even if you do miss transitions, it is easy to ignore out-of-bounds values.

The important things are to have fun with your project, and learn from it.

Thanks for the fast response

So I could easily use any digital pin and write a didcated function for it in order to teremine the speed of the wheen. Pass those into a PID controll function. And then I can happily use pin 20 and 21 for some sensors and a screen?

If they aren't used for anything else (like I2C) , ATmega2560 pins 20 and 21 by Arduino numbering could be used to monitor two speed sensors. Either using interrupts or polling.

Ahhh ok by your last reply I think I get the idea of polling. So I need to figure which pins I could use for polling. I have 4 speed sensors and would like to use I2C as well. Like I wrote in my first message some pins have been used allrady in the current state.

I'll have a search on this

So can one use any pin for polling or only the pins that can be used for interrupts? That is not very clear to me.

"Polling" is merely reading the pin. The fastest way to do that with pin 21 on the ATmega2560 (Port D, pin 0) is this, which takes 1 or 2 machine instructions, depending on what you do with the value.

byte PD0_value = PIND&1;

value = digitalRead(21) would not work? If set with pinMode(20, INPUT_PULLUP)?

Of course digitalRead works. It takes several microseconds, versus PIND, which takes 62 nanoseconds. Look up "direct port access" for Arduino.

ahhh I get your point cool thats good info. Like this I can read the other 3 sensors in "one" time because they are on the other port. Another thing I need to check cause I did not work with reading the full port.

PIND reads the entire port D, so it reads both pins 21 and 20 at the same time. The individual values are

byte x = PIND;
byte pin21_val = x&1;
byte pin20_val = x&2; // or (x&2)>>1 to normalize

Yes thats what I meant so this "saves" time.

And this I should do for 100ms en count the amount of pulses so I can compute the revolutions/min. Like this I get exactly the same as with some code I saw with interrupts?

Many ways to code that. I time the passage between adjacent slots of the speed sensor, which is enough for a reasonable estimate of the speed, especially using a running average.

Hmm yeah thats also an option maybe even a better one.

Do you use some PID control as to make sure all wheels are running at the same speed?

As soon as the car deviates from straight course the 4 wheels will have different speeds. Forcing them to even speed will result in slip and consequently arbitrary motion. 2 driven wheels should be enough for a beginner.