Go Down

Topic: Arduino Mega2560 - Hardware Serial and Servo libraries conflict (Read 1 time) previous topic - next topic

Djnardy01

Hello to everyone, first of all, I appologize because this is my first post ever on this forum and I don't speak very good english (and writing is much bad :D)
Let's start with the beginning: I am working to an autonomous car model, witch shoud send to the remote some data, i.e. speed, direction, etc.
I chosed as communication method, two bluetooth modules. With other words, I've done with the technical platform, and now, the beautiful part: software programming..

Well, I decided to use Arduino Mega2560 because of four serial ports, all used:
Port 0, USB, I'll use this for debug and programming, well, this is unused for now.
Port 1, I use for the bluetooth module, so TX and RX.
Port 2, only RX is used: I've chosen two ATTiny85: This, port is used to read the RPM of the car, to later calculate the speed.
Port 3, only TX, the second ATTiny85, witch will control some LEDs. I've chosed to work with 2xATTiny85 because one use some delay() functions, and other to listen only for pulses to calculate the RPM.
Both tiny.s are using SoftwareSerial on pin3.
So good, so far.
After I assembled everything, first test, of RPM.
And here my problem rised:
I've combined 2 default sketches of arduino IDE to control the ESC, and read RPM from the tiny.

Code: [Select]

#include <Servo.h>
Servo ESC;
int val;
int RPM;
void setup()
{
  // initialize both serial ports:
  Serial.begin(9600);
  Serial2.begin(9600);
  pinMode(13, OUTPUT);
}

void loop()
{
  ESC.attach(46);
  val = analogRead(A0);
  val = map(val, 0, 1023, 1000, 2000);
  ESC.writeMicroseconds(val);
  if (val == 1500)
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }

  // read from port 1, send to port 0:
  if (Serial2.available()) {
    int inByte = Serial2.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial2.write(inByte);
    Serial.println(RPM);
  }

}



If the motor does not rotate, I receive "0" rpm, as exected; but when Arduino starts to send pulse to the ESC (as servo), serial become full of garbage, where I need to reset everything to go back to 0..
 I.ve readed many topics where other users said to change the pin of the servo. I've tried all pins that can generate PWM (any timer on Mega) but without succes..
There are any solutions for this issue ?  :(

PaulS

Quote
Port 0, USB, I'll use this for debug and programming, well, this is unused for now.
Because you don't have any problems that need debugging?

Code: [Select]
  // read from port 1, send to port 0:
  if (Serial2.available()) {
    int inByte = Serial2.read();
    Serial.write(inByte);
  }

The code does NOT do what the comment says.

Code: [Select]
  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial2.write(inByte);
    Serial.println(RPM);
  }

The code does NOT do what the comment says.

Why are you mixing write() and print(ln)() calls to the same device?

Where do you ever assign a value to RPM?
The art of getting good answers lies in asking good questions.

JCOM

where the zuma (floor sweeper) meets surface location?

Because you don't have any problems that need debugging?


Where do you ever assign a value to RPM?
I have a similar project, compare R/C -arduino mega 2560 2x, Arduino Pro Mini 328 - 5V/16MHz 2x, android cell and win10/win-phone BLU win8.1, the PS3 controller is a first try thing still getting documentation.


https://disqus.com/home/channel/rcarduino/discussion/channel-rcarduino/rc_resources/
come see my new project mercy https://disqus.com/home/channel/rcarduino/discussion/channel-rcarduino/rc_resources/
cheers to the innovations

Djnardy01

Hello and thank you for the answers :)
After some research,  I was able to find the problem. It was a (very) poor connection between the grounds of Arduino and the little ATTiny :D
Now it works prety good (up to 15.000 rpm ^_^ ), but I encountered other problems :)) but I will post them in another topic, because I still wave a lot of questions without an answer..
Thank you again :)

Go Up