laptop to arduino over serial

this is my second attempt at this. I want to send colour data from my computer to the arduino over serial I've tried twice but with not much luck the serial is working but I'm not getting the right data into the arduino here is my code.

int colourData;// for use on rainbowduino direct buffer rendering
int dataArray[8];// to hold bits from serial

void setup() {
Serial.begin(9600); //set serial to 9600 baud rate
}

void loop(){
if (Serial.available() > 8) {
getColourData();
}

}

void getColourData(){
for (int i=0; i <=8; i++) {
dataArray = Serial.read();

  • }*
  • colourData = dataArray[0],dataArray[1],dataArray[2],dataArray[3],dataArray[4],dataArray[5],dataArray[6],dataArray[7];*
    }
    As you can see I'm putting the serial data into an array but I'm sure there is a better way I need colourData to contain a hex value I'm hoping to use this function on my rainbowduino to do direct buffer rendering Please can someone show be the best way to go about this.

I want to send colour data from my computer to the arduino over serial

First thing to answer is how are you sending the data? As bytes? As text?

int dataArray[8];// to hold bits from serial

Is this "bits" in the generic sense, or do you actually think that you are storing one bit in a 16 bit variable in an array?

 for (int i=0; i <=8; i++) {
   dataArray[i] = Serial.read();
 }

And if there aren't 8 bytes available to read on each pass through loop, what happens?

 colourData = dataArray[0],dataArray[1],dataArray[2],dataArray[3],dataArray[4],dataArray[5],dataArray[6],dataArray[7];

While this is valid C syntax, it does not do what I think you think it does.

What are you then going to do with colourData?

Thank you for the reply yes I know this is really wrong and I need loads of help with this here is my visual studio code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
//using System.Windows.Controls;

namespace Arduino_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM5";
serialPort1.BaudRate = 9600;
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button5_Click(object sender, EventArgs e)
{
serialPort1.Open();
if (serialPort1.IsOpen)
{
serialPort1.Write("0xff");

}
serialPort1.Close();
//panel1.BackColor = Color.Lime;
//panel2.BackColor = Color.Transparent;
}

private void button6_Click(object sender, EventArgs e)
{
serialPort1.Open();
if (serialPort1.IsOpen)
{
serialPort1.Write("0x00");

}
serialPort1.Close();
//panel1.BackColor = Color.Lime;
//panel2.BackColor = Color.Transparent;
}

private void panel1_Paint(object sender, PaintEventArgs e)
{

}

private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{

}

}
}

I'm hoping to insert the colourData into the rainbowduino's buffer array for direct buffer rendering the 8 bit hex value holds the colour data for two led's 4 bits each Please point me in the right direction on the best way to go about this.

Your sender is only sending one byte. Yet, without checking how many bytes are available, you try to read 8 bytes (that's 64 bits).

You need to check that there is at least one byte (that's 8 bits) to read, before trying to read anything:

if(Serial.available() > 0)
{
   byte colourData = Serial.read();

   // Now, do whatever it is you want to do with the 8 bit value colourData
}

Thanks for your help but still can't get it to work I think I need to go and rethink the project and try and find a better way and also learn more about data types Thanks again.

Hi,
Looking at your code, you appear to belive that you are reading 'bits' with the Serial.read() function. You are not! You are reading a 'byte' (i.e eight bits). So you don't want to read 8 times, just once.

Change the 'int colourData' into 'byte colourData' and basicly do what PaulS say's.

From reading Greg's PC-side code, it appears that he is sending the ASCII characters '0', 'x', 'f', and 'f'. If he sends that twice from two button pushes, that would be eight bytes, eh?

Greg, the way your program on the PC is written, your Arduino sketch will need to read characters into a character array, then convert them to an integer value. You might look at 'sscanf()' for the conversion.

Regards,

-Mike

Mike...

You are quite right! I missed the quotes in his sending routine (silly me - it's been a long day :wink: )

BTW... it's not my code, it's andy's.

Andy... leave the quotes out of your sending routine so that you actually send the hex (byte) value you thought you were. 0xff not "0xff" for example.

Ok thank you both for your help I'm going to try Qt to do the windows programming side as I don't really like visual studio i'll get them quotes out and retry the arduino.