Asked — Edited

Uart Connecting To An Arduino

I'm very new to both the ez-b4 and Arduino and need basic help or advice. With my robot I want to be able to control LEDs through an Arduino. I have a Mega and with it I can add more LED functions to it as I learn. I've been reading a lot of posts on this and I took in so much info, I think I'm just dangerous and not knowledgeable.

I want to start simple, just turn on and off LEDs through my Arduino. I eventually want to control more LEDs and do more than just turn on and off.

Right now my Ez-b is powered through my main power source at 7v. My Arduino is plugged into my laptop and powered with USB, once mounted in my robot, an R2, it will run off the same power source, not through the Ez-b, and will power up when the Ez-b powers up.

Using UART 0 I connect the TX and RX to TX and RX of the Arduino. I'm unsure if it matters which ones. TX0, RX0 or any of the other numbered through 1-3, pins 14-19.

I found a nice Arduino code to get me going for loading onto the Arduino.


/*
 2  Simple LED sketch
 3  */
 4 
 5 int led = 13; // Pin 13
 6 
 7 void setup()
 8 {
 9     pinMode(led, OUTPUT); // Set pin 13 as digital out
10 
11     // Start up serial connection
12     Serial.begin(9600); // baud rate
13     Serial.flush();
14 }
15 
16 void loop()
17 {
18     String input = "";
19 
20     // Read any serial input
21     while (Serial.available() > 0)
22     {
23         input += (char) Serial.read(); // Read in one char at a time
24         delay(5); // Delay for 5 ms so the next char has time to be received
25     }
26 
27     if (input == "on")
28     {
29         digitalWrite(led, HIGH); // on
30     }
31     else if (input == "off")
32     {
33         digitalWrite(led, LOW); // off
34     }
35 }

I still have a bunch to read up on the scripting part in ARC and how to actually send the on off command, but first, how do I correctly connect the two devices together? I know I can connect LEDs to the Ez-b, but it will use up ports and I want to expand my use of LEDS.

Thanks and sorry forwhat I'm guessing is probably very basic question.


ARC Pro

Upgrade to ARC Pro

Stay at the forefront of robot programming innovation with ARC Pro, ensuring your robot is always equipped with the latest advancements.

#1  

And a ground to both boards.

#2  

Quote:

And a ground to both boards.

To each other, since they are grounded through the power right? To any ground on the Mega, I've grounded the LEDs at the ground next to pin 13

#3  

I recommend looking at post 24 at https://synthiam.com/Community/Questions/10451&page=3

You have to have a common ground for the EZ-B to communicate to the Arduino.

You can do this from any digital or analog pin on the EZ-B, or from the UART port. TX from the EZ-B (or any digital pins signal wire) to the Arduino can carry the send data from the EZ-B to the Arduino on its RX pin.

If you need to receive data on the EZ-B, you need to have the TX pin from the Arduino connected to the RX pin on the EZ-B. At this point it also is logical to use the UART port on the EZ-B for this and use the TX port on the EZ-B to connect to the arduinos RX port.

Ground is Ground so it doesnt matter which ground pin you use on the arduino or EZ-B but for simplicity sake, I would use the ground pin that matches the signal pin on the EZ-B (digital or uart depending on which way you wire) to the ground pin that is beside the TX and RX pins on the Arduino.

To see how to send data to the Arduino, and then catch it and process it in the Arduino sketch, I would look at the thread posted above for examples.

#4  

Ok, I have it connected but I'm sending my command to the wrong pin. I have connected to UART0

Now when I send the command

sendSerial(0, 9600, "on")

The servo I have plugged into D0 clicks. How to I find what I'm doing wrong?

Thanks

#5  

If you are connected to the uart port of the ezb then why are you using sendserial commamds? It is also not in the proper format anyway... I am not sure what you are trying to do but if you are trying to send commands to your arduino you need to use the uart command ( since your arduino is connected to it... Or are you trying to move a servo plugged into the ezb? I am on my phone so i have to show the code this way Uartinit(0,0,9600) UartWrite(0,0,"on") I am away from my pc so i can't confirm the right syntax at the moment...

#6  

If you are just trying to move a servo plugged into D0 on your ezb the correct syntax is.... Servo(D0,90) # moves the servo to 90 deg

#7  

as Richard stated, if you are connecting to the UART port and not a digital port, the command you are using wont work. I am in my shop right now and dont have access to ARC. In the script you have an option to see the available commands. You can search for UART and see the available commands for the UART port. This should get you there.

#8  

I'm using UART and the wrong command, thanks.

#9  

Okay, back at my desk now.

I haven't tested this out but I would do something like this in a script.


if(UARTAvailable( 0, 0 ))
    UARTWrite( 0, 0, "Whatever you want to send here" )
else
   UARTInit( 0, 0, 9600 )
   Sleep(500)
   if(UARTAvailable( 0, 0 ))
      UARTWrite( 0, 0, "Whatever you want to send here" )
   else
      SAY("I can't communicate with my light controller")
   end if
end if

#10  

I get an error message-

Error on line 1: Missing 2 closing statement (ENDIF/ENDREPEAT/ENDREPEATUNTIL/ENDREPEATWHILE/etc)
Done (00:00:00)

I tried running just

 UARTWrite( 0, 0, "on" )

I get a done message, but my lights don't turn on. They are plugged in correct, I tested them before changing it to the code above. If I reupload the sketch, the light on pin 13 blinks.

I put the UARTthe Init( 0, 0, 9600 ) in the connection script area.

PRO
Synthiam
#11  

Dave, your code in the previous response doesn't make sense:). Checking UartAvailable() returns the number of bytes in the input buffer.

Also, kashyyk, the error you are receiving is telling you exactly what is missing. Read the error and understand each word in the sentence. Just like you are reading my response, the error message is also a sentence. It's telling you something:)

PRO
Synthiam
#12  

Don't use words for commands like "ON" or "OFF". Create a protocol instead. If you simply use a BYTE (0-255), then you have 255 different commands to send. You can make it so some commands have parameters, but don't use the word "ON" or "OFF", that's getting into writing console commands for user interfaces and entirely out of scope for the question.

Look at the EZ-B protocol if you need an example of what a protocol looks like. You can view it here: https://synthiam.com/Tutorials/Lesson/18?courseId=4

Here is working sample code...

ARDUINO


int led = 13; // Pin 13

void setup() {

  pinMode(led, OUTPUT); // Set pin 13 as digital out

  // Start up serial connection
  Serial.begin(9600); // baud rate
  Serial.flush();
}

void loop() {

  // Wait until there is data
  while (Serial.available() > 0) {

    int input = Serial.read();

    if (input == 33) 
      digitalWrite(led, HIGH); // on
    else if (input == 44)
      digitalWrite(led, LOW); // off
  }
}

EZ-SCRIPT


UARTInit(0, 0, 9600)

:loop

  sayezbWait("I am turning on LED")
  UARTWrite(0, 0, 33) 

  sleep(4000)

  sayezbWait("I am turning off LED")
  UARTWrite(0, 0, 44) 

  sleep(4000)

goto(loop)

#13  

Thanks for the correction DJ. What I was doing in my C# project didnt transfer well here.

#14  

Thanks DJ! I'll try it out later this afternoon.