Hello All. I have been working with Arduino and another micro-controller called the uVGA-II made my 4D-Systems as part of an Independent Study project at my high school. I seem to have hit a wall and my instructor lacks the knowledge to give me the aide I need so I have come to these forums! What I am attempting to do is to read a number from the serial monitor and store the number on my Arduino Mega. I did some reading and believe I am retrieving the numbers correctly from the serial monitor. I then want to send this int over a serial connection to the second micro-controller. To do this I was trying to use bit shift operators to send the least significant byte then the most significant byte (I may have those confused. This is where I am stumped). I then attempt to receive those numbers on the uVGA-II and turn them back into an int by reversing what I did with the bit shift earlier. Once I have the int on the uVGA-II I print it to a VGA monitor.
This is the code I have on the Arduino Mega
int toPrint;
void setup()
{
Serial.begin(19200);
Serial3.begin(19200);
Serial3.flush();
delay(1200); // wait for uVGA to power up
}
void loop()
{
if(Serial.available() > 0)
{
toPrint = serReadInt();
}
Serial.println(toPrint);
send16(toPrint);
}
int serReadInt()
{
int i, serAva; // i is a counter, serAva hold number of serial available
char inputBytes [7]; // Array hold input bytes
char * inputBytesPtr = &inputBytes[0]; // Pointer to the first element of the array
if (Serial.available()>0) // Check to see if there are any serial input
{
delay(5); // Delay for terminal to finish transmitted
// 5mS work great for 9600 baud (increase this number for slower baud)
serAva = Serial.available(); // Read number of input bytes
for (i=0; i<serAva; i++) // Load input bytes into array
{
inputBytes[i] = Serial.read();
}
inputBytes[i] = '\0'; // Put NULL character at the end
return atoi(inputBytesPtr); // Call atoi function and return result
}
else
return -1; // Return -1 if there is no input
}
void send16(word value)
{
Serial.print(value);
Serial.print(" ");
Serial.println(value >> 8);
}
And this is the code on the uVGA-II. It is written in a language called 4DGL.
#platform "uVGA-II_GFX2"
#constant PORT COM0
#constant BAUD_RATE 19200/10
#constant BUF_SIZE 200
func main()
var toPrint;
var private combuf;
repeat
if(!combuf)
com_SetBaud(PORT, BAUD_RATE); // set port and baud rate
combuf := mem_Alloc(BUF_SIZE); // allocate memory for com buffer
#IF PORT == COM0
com_Init(combuf, BUF_SIZE, 0); // set up the buffered comms
#ELSE
com1_Init(combuf, BUF_SIZE, 0); // set up the buffered comms
#ENDIF
endif
if(com_Count >= 2)
toPrint := getWord();
endif
print(toPrint);
forever
endfunc
func getWord()
var a, w;
gosub getByte;
a := w;
gosub getByte;
w := a | w<<8;
getByte:
while((w:=serin()) < 0);
endsub;
return w;
endfunc
I have really hit a wall here. The issue I'm having is that I can receive the input from the serial monitor, it stores as an int and then I attempt to send it as two bytes. When I attempt to display it all I get is 0's which I'm assuming means the signal isn't being sent properly. I currently have TX3 (pin 14) attached to the RX pin on the uVGA and I have RX3 (pin 15) attached to the TX pin on the uVGA. I won't be able to stay much longer tonight so I will answer any questions about clarification tomorrow morning. I just wanted to get this up because I haven't made much progress in the past week dealing with this issue. Thank's to everyone who helps out ahead of time. If my project turns out the way I hope it will I might come back and put up some sort of follow up. Thanks!