Hello
First please could I say that after two years of great fun with Arduino's this is my first post here on the forums! Shameful really, but thanks for the help I've had/read over that time.
Project overview:
So, rather than be cryptic about what I'm trying to achieve, I'll start by saying that I'm building a digital scoreboard for my cricket team. The idea is that I will have a shift register controlling an array of LED's for each digit on the scoreboard, and interface with these via an Android app over bluetooth using (HC-06).
Prototype details:
At the moment I have a buggy but working Android app (it's only a prototype, I'll gut it and start a-fresh when I've got things working properly) and a simple, yet also inefficient Arduino sketch for testing.
Because I've not ordered the shift registers yet, or the rest of the materials for creating the digit displays for that matter, I am instead just flashing a set of 6 LED's. For example, if I were to set the score to 100 (for arguments sake, LED 2) then the corresponding LED would simply flash 100 times when it was called in the flash loop.
Problem description:
In step by step form:
- Turn Arduino on with battery and bluetooth, no wired connection, HC-06 flashes while waiting for connection, LED's flash once each (twice actually, need to figure out why sometime) in a continuous loop
- Android app connects to HC-06, initializes deviceSocket, HC-06 light becomes constant
- Android app sends data to Arduino, led loop hangs while the Serial.read() commences
- Arduino continues without change, no change in number of flashes for any of the LED's
What I'm trying to do is send the following byte array from the app
'{','c','}',pinNumber,pinValue
. pinNumber being the pin that I want to affect, and pinValue being the number of flashes to do (I recognize this means I cannot input a score over 127, please advise on how I could overcome this?
)
What should have happened after the data was transmitted was that pin2 should've flashed 60 times, while all others remained on 1 flash.
I'm finding it hard to track down my issue as it is very difficult to get any kind of idea what bytes are arriving at the Arduino when the app is communicating with it. I am still considering writing a C# windows app on my computer to display the received bytes from the Android app and will do this if we can't figure it out ![]()
Code:
Arduino:
#define NUM_PINS 6
#define PIN_START 2
#define BUFFER_LEN 3
#define BAUD_RATE 9600
class Pin {
private:
int id;
float pause = 30;
float flashTime = 1000;
int value = 0;
public:
void assignValue(int val)
{
value = val;
}
void initialize(int i)
{
id = i;
pinMode(id, OUTPUT);
activate();
}
void activate()
{
flash(id, value);
}
};
class Communicator {
private:
char buffer[BUFFER_LEN];
public:
int lastBytes[2] = { -1,0 };
void initializeSerial(int baud)
{
Serial.begin(baud);
}
void getPinData()
{
if (Serial.available() > 0)
{
if (Serial.read() == '{')
{
if (Serial.read() == 'c')
{
if (Serial.read() == '}')
{
int pinNum = Serial.parseInt();
int pinVal = Serial.parseInt();
lastBytes[0] = pinNum;
lastBytes[1] = pinVal;
}
}
}
else
{
while (Serial.peek() != '{')
{
Serial.read();
}
}
}
}
};
Pin *pins = new Pin[6];
Communicator *comm = new Communicator();
void setup()
{
comm->initializeSerial(BAUD_RATE);
// Debug LED pin
pinMode(13, OUTPUT);
// Setup input pins (not sure this is actually necessary)
pinMode(0, INPUT);
for (int i = 0; i < NUM_PINS; i++) {
int v = i + PIN_START;
pins[i].assignValue(1);
pins[i].initialize(v);
}
}
void loop()
{
// Get data from serial
comm->getPinData();
// If we were assigned pin data for a specific pin, assign it
if (comm->lastBytes[0] > 0)
{
pins[comm->lastBytes[0]].assignValue(comm->lastBytes[1]);
}
for (int i = 0; i < NUM_PINS; i++)
{
pins[i].activate();
}
}
void flash(int pin, int numOfFlashes)
{
for (int i = 0; i <= numOfFlashes; i++) {
delay(50);
digitalWrite(pin, HIGH);
delay(50);
digitalWrite(pin, LOW);
}
}
Android (Only relevant transmit thread):
public class sendDataThread implements Runnable{
OutputStream oStream;
byte[] buffer;
public void run(){
Log.d("Debug", "Thread started with: " + buffer[0] + " and " + buffer[1] + " then " + buffer[2] + " ends with: " + buffer[3]);
try {
//TODO: Un-fuck this bit
oStream.write(buffer);
} catch (IOException e) {
}
}
public sendDataThread(byte p, byte v, OutputStream os){
// Construct the buffer to transmit, we start with '{c}' to tell the Arduino to start listening.
// The buffer therefore looks something like '{c},2,128'
buffer = new byte[]{'{','c','}',p,v};
Log.d("GTStudios",buffer.toString());
oStream = os;
}
}
Thank you all in advance, sorry if this is a rather long post but I wanted to help you help me as much as possible ![]()