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.
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;
}
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.
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.
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.