Alright, I finally got my key working.
So would I be best off programming the microcontroller with the drag and drop gui or c code?
How good are you at programming C or C++ or Java?
Alright, here is my setup:
I have an Arduino Uno R3 with a potentiometer hooked onto A0, and a led on D13. It will send the pot value to the Vex PIC, and it will read the PIC.
On the PIC, I have a motor on Motor port 1, and a limit switch on D1. It should be sending the limit switch, and reading the pit value from the Arduino.
The Arduino sends the pot value to the Vex, and the Vex reads it and updates the motor. The one thing that doesn’t work is the Arduino lighting the led when the limit on the Vex is pressed.
Vex code:
// everything.c : implementation file
#include "API.h"
// TODO: add user code here
//#include "Main.h"
void stuff ( void )
{
unsigned char tx = 0; // Byte to be transmitted
//unsigned char rx = 0; // Byte we received
unsigned long rx_count = 0; // Count of bytes received
unsigned long tx_count = 0; // Count of bytes sent
int switchD1=0; //1 not clicked, 0 clicked
int potentiometer=127; //Value from arduino
// First, set up the RX/TX Port for use
OpenSerialPortTwo ( BAUD_9600 ) ; // Select your baud rate here
while(1){
// Check for available RX data
while(GetSerialPort2ByteCount()){
potentiometer=ReadSerialPortTwo();
rx_count++; // Count each byte we receive
}
switchD1 = GetDigitalInput(1); // Get the switch
SetPWM(1, potentiometer); // Set motor 1 to the potentiometer value
// Lets TX a byte
WriteSerialPortTwo(switchD1);
tx_count++; // Count each byte we send
// And lastly, update the Graphic Display once every 256 bytes
Wait ( 16 ) ;
}
}
Arduino code:
int pot=127;
int light=0;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available() > 0) {
light=Serial.read();
}
if (light==1){
digitalWrite(13, HIGH);
}else if(light!=1){
digitalWrite(13, LOW);
}
pot=analogRead(A0);
pot=map(pot,0,1023,0,255);
Serial.write(pot);
delay(16);
}
Does anyone see anything wrong with my code? I think it is more on the Arduino side, not the Vex side, but I could be wrong.
Now I think it is the Vex code. I took out the TX line on the Arduino, replaced Serial.write(pot); to Serial.println(light);, and all it was printing to the screen was zeros, even if I hit the switch on the Vex. I made sure to make sure the Vex is set up with Digital 1 as an input as well.
EDIT: The Arduino wiki says that if no data is available via Serial.read, it would output -1. It is outputting 0, so there is data being communicated. I think it is Vex.
If you leave the Vex Programming Cable connected and switch to the Terminal Screen of the Downloader, what do you see??
WriteSerialPortTwo(switchD1);
Does this write Chars ( 8 bits ) or Ints ( 16 Bits )??
switchD1 is an Int, maybe it need to see just a Char, ( or a Short Int ).
Try Casting it:
WriteSerialPortTwo((char *) switchD1);
It doesn’t output anything. I have a few ideas why:
-The baud rate on the terminal is 115200, and I set the output on my program to 9600.
-It is outputting it to Serial2. I think Serial1 is the computer/program port.
Ahh… You are using the TX/RX port on the PIC to communicate with the Arduino…
Try the Cast… I would guess that the Wrong Byte of the Int is being sent…
You can also make [FONT=Courier New]switchD1[/FONT] an [FONT=Courier New]unsigned char[/FONT]…
Nope, nothing happens.
I added a Print to screen to my code (Vex) to see if it was reading the switch right, and it is. It has to do with sending it over Tx then, I think.
Wow, turn my back for a couple of hours and finally some activity on the forums.
you need to set the arduino pin mode in setup
void setup() {
// initialize the digital pin as an output.
pinMode(13, OUTPUT);
Serial.begin(9600);
}
You may also have a conflict between the Arduino debug serial port and the PIC, I assume you download the program, then disconnect from the USB and hook the serial port to your PIC, but now the RX line comes from two places, the PIC and the USB RX, is that your setup ? You may need to use the softwareSerial library on the Arduino to get around this.
I completely forgot about the pinMode thing. Yes, when I run the Arduino and pic together, I take out the USB on the Arduino. When I read the serial from the Arduino, I take out the tx line and change the code to print to the screen.
To make life easier and allow debugging I suggest you use the SoftwareSerial library which is included by default these days. You use it pretty much the same as the built in serial port but with some small additions. It’s no good for high baud rates but for 9600 will be ok.
assign the two pins you want to use.
SoftwareSerial mySerial(10, 11); // RX, TX
setup
mySerial.begin(9600);
example read and write
char c;
if (mySerial.available())
c = mySerial.read();
mySerial.write(c);
So your setup function would be, choose your own pins.
SoftwareSerial mySerial(10, 11); // RX, TX on pins 10 and 11
void setup() {
// initialize the digital pin as an output.
pinMode(13, OUTPUT);
// debug
Serial.begin(9600);
//to PIC
mySerial.begin(9600);
}
You could then send data received from the PIC to the console for debugging (for example).
char str[32];
if (mySerial.available() > 0) {
light=mySerial.read();
sprintf(str, "Light = %d\r\n", light);
Serial.write(str);
}
I did not know you could do that. I guess someone wanted to make an UNO a Mega, haha. If it still doesn’t light the led or print anything else to the screen, what would you change on the Vex side?
I will add in the other Arduino features in the morning.
It’s not a real serial port, just bit banging.
The UNO R3 does have 1k resistors in the tx/rx lines to the USB serial interface so it may work, I don’t have an UNO.
On the PIC side, I don’t know, it looks ok but I have never used a PIC. Is the wiring OK? Perhaps try a loopback test. write some test code to output from the Arduno and feedback into itself ie. connect pins 1&2. Then do the same on the PIC side, see if you can read a byte you have written.
Alright, I added some things to the Arduino code, and now the led will light up when the button is pressed. There are a few “problems” though. One, it is slow. Two, there is a lot of noise, like random variables being mixed in to the stream like the pot. Actually, the pot is mixed into the stream every once in a while, so you will see a random variable besides 1 or 0. I’ll post the code in a sec.
#include <SoftwareSerial.h>
SoftwareSerial Serial2(10, 11); // RX, TX
int pot=127;
int light=0;
void setup(){
Serial.begin(9600);
Serial2.begin(9600);
pinMode(13, OUTPUT);
}
void loop(){
if (Serial2.available() > 0) {
light=Serial2.read();
}
if (light==0){
digitalWrite(13, HIGH);
}else if(light!=0){
digitalWrite(13, LOW);
}
pot=analogRead(A0);
pot=map(pot,0,1023,0,255);
Serial2.write(pot);
Serial.println(light);
delay(16);
}