Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Re: Arduino to Arduino Serial Communications on: February 27, 2013, 07:34:19 pm
I made PaulS's changes of just inserting a NULL in position 0 of the buffer instead of zeroing the whole buffer.  No change.  Wiring looks correct

Arduino Mega            Arduino UNO R3
pin 17 rx2    ---------> pin 1 tx
pin 16 tx2    ---------> pin 0 rx
pin GND       ---------> pin GND  (both just above pin 13)

So I guess I will need to purchase another Arduino and try again.

Thanks PaulS for taking the time to test the code on your Arduinos

...pat
2  Using Arduino / Networking, Protocols, and Devices / Re: Arduino to Arduino Serial Communications on: February 27, 2013, 07:04:53 pm
I am new to microprocessors including the Arduino.  I am also new to robots.  The reason I need to do this is that I have joined the Robot Club (FRC) at the High School.  I have an electronics(10 yrs), and programming(30) but a bit rusty and NO Robot background.  My intension is to learn about Robots, MicroProcessors so I can help next years students when the current experts go off to college.  To learn robots I am building a robot with as many sensors as I can use.  Thye Arduino UNO R3 does not have enought Digital PWM ports.  So the Arduino UNO will have the Motor Controller, three Ultrasound range finders... The Arduino Mega will have three passive sensors to start with.  The Arduino Mega will update the Arduino UNO via binary messages over the Serial Port about the state of all sensors connected to it.  The Arduino may need to send messages to the Arduino Mega. Later I will add the "SerialEvent" so the main code can execute without waiting on incoming data.  The memset of the buffer is to insure I am not reading old data.

Because I have NO experience with Robots and Microprocessors each time I add a sensor or feature like communications I write code to learn each sensor or feature without any other functionality.  That is what this code is.  It is to help me understand how to do Serial communications between these two Arduinos.  You are right is does nothing more than bounce a message back and forth.  The Arduino Mega is the original sender.  Once I learn the Arduino I can move on to the cRIO & FRC code provided to the students to build Robots.

I added the numbers at the left because that is what I thought you wanted.  I have made the changes you have suggested.  It is still not working.  Nothing but trash.  You can see bits and pieces of the original message amoung the trash.   It looks as thou they are not communicating at the same speed or with different communication confguration parameters.  (which is why I added the SERIAL_8N1 even tho it is the default)

Here is the updated code on the Arduino UNO R3

Code:
#define BUFLEN 200
char buf[BUFLEN];
void setup()
{
  delay(3000);
  Serial.begin(9600);
  Serial.setTimeout(100);

}/* setup */
void loop()
{
  int cnt = 0;
  cnt = Serial.readBytesUntil('\n', buf, BUFLEN-50);
  if (cnt > 0) Serial.write((uint8_t*) buf, cnt);
  memset(buf,0,BUFLEN);
  delay(3000);
} /* loop */


Here is the updated code on the Arduino Mega

Code:
#define BUFLEN 200
char buf[BUFLEN];


void setup()
{
  Serial.begin(9600);
  Serial2.begin(9600);
  Serial.println("ready to exit setup");
  Serial2.setTimeout(100);
  delay(3000);

}/* setup */


void loop()
{
  int cnt = 0;
  Serial2.write("This is a message from Arduino Mega to the Arduino UNO R3 via the serial port\n");
  cnt = Serial2.readBytesUntil('\n', buf, BUFLEN-40);
  Serial.print(cnt); Serial.print("  --  ");
  if (cnt > 0 ) Serial.println(buf);
  memset(buf,0,BUFLEN);
  delay(3000);
} /* loop */


Here is the output at the Arduino Mega

Code:
ready to exit setup
49  --  is ió mesó froí.VWË땝…ÿ¡•AþWËë þ’Í ÿthe ó+±ðø
46  --  Z.JÍ mesû2ɽµVWËëYVô¡•Aþ]Zë ÿR3 vi¡•sÿX ‚½
49  --  is ió mesû2ɽµñ‘Õ¥¹ý•…ÿ¡•AþWËë ÿ’Í þthe ó+±ðá
49  --  is ió mesû froí.VWË땝…ÿ¡•AþWËë þ’Í vi¡•sÿX ‚½
50  --  is ió mesó2ɽµVWËëÿega ô¡•AþWËë þR3 vi¡•sÿX ‚½ø
49  --  is ió mesó2ɽµ‘Õ¥¹ý•…ÿ¡•AþWËë þ’Í ÿthe ó+±ðá
49  --  is ió mesó froí.VWË땝…ô¡•AþWËë þ R3 ¢¡•sÿX ‚½
48  --  is ió mesó froírduinª…¡•AþWËë þ’Í ÿthe ó+±ðá
49  --  ¥Íiûmesû2ɽÿɑե¹ª…ÿ¡•AþWËë ú’Í þthe sZX ‚á
48  --  ¥Íiûmesû2ɽÿɑե¹ª…¡•AþWËë ú’Í öthe s…±ð)
47  --  ¥Íiûmesû2ɽÿɑե¹ª…¡•AþWËë þ’Í ¢¡•ó+±ð)
52  --  ¥Íiûmesû2ɽÿɑե¹ª…ü¡•AþWËë þ R3 ÿ theॅ±þ—ø
52  --  ¥Íiûmesû2ɽÿɑե¹ÿª…ÿ¡•AúWËë þ R3 theørialàrt
47  --  ¥Íiûmesû2ɽÿɑե¹ª…¡•AþWËë þ’Í ¢¡•ó+±ð)
53  --  +kiûmesû2ɽõɑե¹ÿª…ÿ¡•AúWËë þ R3 theürialàÉÑ)
52  --  +kiûmesû2ɽÿɑե¹ÿª…ÿ¡•AþWËë þR3 X¢¡•eriaüort
51  --  +kiûmesûY2Éýɑե¹ÿª…ÿ¡•AþWËë þR3 X¢¡•á–¥…±KÑ)
52  --  +kiûmesó2ɽÿɑե¹ÿª…ô¡•AþWËë þR3 X¢¡•eriaüort
52  --  +kiûmesó2ɽÿɑե¹ÿª…ÿ¡•AþWËë þR3 X¢¡••É¥…ùort
52  --  +kiûmesó2ɽõɑե¹ÿª…ÿ¡•AþWËë þR3 X¢¡••É¥…ùort
36  --  +kiûmesó2ɽõɑե¹ýª…ÿ¡•Aþ]Zë ÿ

In response to your statement:
Quote
Take it easy with the aggressive tone. We are trying to help. You aren't being very responsive.

I really did not find the tone of PaulS's comment to be pleasant.  
Quote
You really don't understand how NULL terminated strings work, do you? Nor do you seem to even
have a concept what a NULL terminator is.

Think of it as a stop sign. You stop when you get to one, right? Is there any reason to put 2
00 of them on one sign post? Do that make you more inclined to stop? Or does it make the signpost
look less than intelligent?

It came accross to me as condescending, but lets put all that behind us, and let me take advantage of y'all's knowlege of the Arduino.

Thanks for pointing out the error in the ZERO macro, I now see the error in it.

thanks...pat


I just saw PaulS's new comment.  Thanks.  The only difference now is that I do the memset instead of just inserting the NULL at position 0.  I will try that additional change.  I do not think it should make a difference, but sometimes thing become clear after you understand the error.  If that does not fix it then it is likely something electrial or with the boards.
3  Using Arduino / Networking, Protocols, and Devices / Re: Arduino to Arduino Serial Communications on: February 27, 2013, 01:39:08 pm
I'll accept your comment about the wait for Serial2, but it just drops through the while statement during execution.

As to the use of the Serial.write instead of Serial.print, ALL data is binary.  My reason for using them here is that this is just test code to get it working, and the eventual use will be binary (non-ASCII).

Neather of your comments solve the problem, nor are they very useful.
Since you know so much solve the problem.  It would be much appreciated.

...pat
4  Using Arduino / Networking, Protocols, and Devices / Re: Arduino to Arduino Serial Communications on: February 26, 2013, 09:28:49 pm
yes, memset would do the same thing, but it is an old habbit out of 30 years of programming.  but thanks my code could always use a few good suggestions.  Now back the real problem of Arduino to Arduino Serial communications.
5  Using Arduino / Networking, Protocols, and Devices / Re: Arduino to Arduino Serial Communications on: February 26, 2013, 09:12:59 pm
the purpose was not to null terminate the string, but to clear out all data.  You remark does nothing to answer the problem.  Why don't you try to solve the problem.
6  Using Arduino / Networking, Protocols, and Devices / Arduino to Arduino Serial Communications on: February 26, 2013, 12:55:23 am
I have a Arduino Mega & an Arduino UNO R3 that I am trying to get to talk together.
Serial2 on the Mega and Serial on th UNO
TX is connected to RX on other unit
Grounds are connected together.
UNO is powered via 9V Battery
Code is downloaded to UNO then USB cable is disconnected & the connections to  TX RX on UNO are connected
Code then downloaded to Mega
press RESET on Arduino UNO R3 to execute setup function again
Note I have been able to write from Mega SERIAL1 to same MEGA Serial2 and it worked
Nothing but trash (Such as:  59  --  his is j•ÍͅɽµAr]Zë jõto thɑե¹½O R3 ZX¢¡••É¥…±ÉÑ ) is received on Mega when connected to UNO


If anyone can help I would appreciate it.
thanks...pat

Code on MEGA is as follows:

Code:
1    define ZERO(buf,len) for (int i = 0; i <= len;i++) buf[i] = 0  // clear data from buffer
2    #define BUFLEN 200
3   char buf[BUFLEN];
4
5
6   void setup()
7   {
8     Serial.begin(9600, SERIAL_8N1 );
9     Serial2.begin(9600, SERIAL_8N1 );
10    while (!Serial2)
11    {
12      ; // wait for Serial2 port to connect. Needed for Leonardo only
13    } /* while */
14    // reserve 200 bytes for the inputString:
15    Serial2.write("This is a message from Arduino Mega R# to the Arduino UNO R3 via the serial port\n");
16    Serial.println("ready to exit setup");
17    Serial2.setTimeout(100);
18    delay(3000);
19
20  }/* setup */
21
22
23  void loop()
24  {
25    int cnt = 0;
26    cnt = Serial2.readBytesUntil('\n', buf, BUFLEN-40);
28    if (cnt > 0) Serial2.write((uint8_t*) buf, cnt);
29    if (cnt > 0 ) Serial.println(buf);
30    if (cnt <= 0 ) Serial2.write("This is a message from Arduino Mega R# to the Arduino UNO R3 via the serial port\n");
31    Serial.print(cnt);
32    ZERO(buf,BUFLEN);
33    delay(3000);
34  } /* loop */

Code on UNO is as follows:

Code:
1   #define ZERO(buf,len) for (int i = 0; i <= len;i++) buf[i] = 0   // clear data from buffer
2   #define BUFLEN 200
4   char buf[BUFLEN];
5   void setup()
6   {
7     delay(5000);
8     Serial.begin(9600, SERIAL_8N1);
9     Serial.setTimeout(100);
10
11  }/* setup */
12  void loop()
13  {
14    int cnt = 0;
15    cnt = Serial.readBytesUntil('\n', buf, BUFLEN-50);
16    if (cnt > 0) Serial.write((uint8_t*) buf, cnt);
17    ZERO(buf,BUFLEN);
18    delay(3000);
19  } /* loop */
20
=============================End of MSG=====================================
7  Using Arduino / Motors, Mechanics, and Power / Re: Arduino Motor Sheild & DC Motor on: January 30, 2013, 03:53:12 pm
Yes it did help.  Thanks.  Based on it I was able to get variable speed from the motors.  Simplified code follows:



#define DIRA            12                  // Direction motor pin
#define DIRB            13                  // Direction motor pin
#define PWMA            3                   // PWM motor pin
#define PWMB            11                  // PWM motor pin
#define BREAKA          9                   // Break A
#define BREAKA          1                   // Break B
#define CURRENTA        A0                  // Current Sensing A
#define CURRENTB        A1                  // Current Sensing B


int PWM_val = 0;                                // (25% = 64; 50% = 127; 75% = 191; 100% = 255)


void setup()
{
  analogReference(EXTERNAL);                            // Current external ref is 3.3V
//  Serial.begin(115600);
  Serial.begin(9600);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(CURRENTA, INPUT);
  pinMode(CURRENTB, INPUT);
  digitalWrite(CURRENTA, HIGH);                     
  digitalWrite(CURRENTB, HIGH);

  analogWrite(PWMA, PWM_val);
  analogWrite(PWMB, PWM_val);
  digitalWrite(DIRA, HIGH);
  digitalWrite(DIRB, HIGH);
  delay(5000);
} /* setup */

void loop() {

  for (PWM_val = 0; PWM_val <= 255; PWM_val = PWM_val +10)
  {
    analogWrite(PWMA, PWM_val);                                               // send PWM to motor
    analogWrite(PWMB, PWM_val); 
    Serial.print("PWM:");          Serial.print(PWM_val);  Serial.print("\n");
   delay(2000);
  } /* for */

 } /* loop */

8  Using Arduino / Motors, Mechanics, and Power / Arduino Motor Sheild & DC Motor on: January 30, 2013, 01:09:23 pm
I can not find anything on the WEB about using the Arduino Motor Sheild to control a DC Motor.  My understanding is that it can control 2 DC Motors.  I asume the motor would be connected to either the A or B two screw downs on the blue block.  If not correct then how?

After I plug the Arduino Motor Sheild into the Arduino, how do I program the Arduino then.

thanks...pat
pjk3(at)pdq.net
Pages: [1]