Pages: [1]   Go Down
Author Topic: USB C++ signals lost when sent to the Arduino Uno board  (Read 1246 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody,

I have a few questions that I have not been able to find the answers to, so I was hoping that somebody could point me in the right direction.

So for my project I am sending data to my Arduino Uno from C++, and the Arduino will output a PWM signal to a one of three pins depending on the signal that is sent to it.

The sketch does work correctly when the data is sent slow enough, (like when I test my program with the Arduino Sketch Editor's Serial Monitor or when I run my C++ program to send single signals to the board).
However, when I send signals to fast it loses anywhere from a few signals to all of the signals (Ideally I would like to send at least 90 signals a second).  
But to make things more confusing, I am sending data out on three pins (9, 10, & 11).  Pin 11's signal is always correct, whereas the other pins have random values assigned to the pin. (And yes I have made sure multiple times that the correct data is being sent).

At first I thought that the pin 11 is receiving the data correctly because the fact that I send three signals per program loop, and with pin 11 being the last, it had more time to recieve the data while the second loop begins.
To test this I switched the order that my program sends pin 10 and 11, and 11 is still correct, while 10 will have seemingly random values.

So I was wondering if anybody knows why this is?  Also I was hoping that somebody could please point me towards a signal handshaking protocol with the Arduino.

Thanks for reading.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33295
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to post the sketch you have before we can tell. Either you are not using serial available correctly (or at all) or you are not getting round your loop fast enough. When you post the sketch use the # icon
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int input = 0;
int offset;
int repeat;
int position = 0;
int val[10];
int out_value;
int digit_offset = 0;
int counter = 0;
int arraysize;
int endrepeat;
int mfc;
int pin[6] = {
    3, 5, 6, 9, 10, 11
};
int pulsewidth[6] = {
    0
};
int pulsenow[6] = {
    0
};
void setup() {
    Serial.begin(9600);
    for (int i = 0; i < 6; i++) {
        analogWrite(pin[i], 0);
    }
    Serial.print("");
}
void loop() {
    while (Serial.available() > 0) {
        delay(1);
        input = Serial.read();
        if (input == 10 || input == 13 || input == 23) break;
        else if (input >= '0' && input <= '9') {
            input = input - '0';
            val[counter] = input;
            counter++;
        }
    }
    if (Serial.available() == 0 && counter > 0) {
        arraysize = counter;
        digit_offset = 0;
        endrepeat = arraysize - 2;
        out_value = 0;
        position = 0;
        while (position < arraysize - 1) {
            repeat = endrepeat;
            digit_offset = val[position];
            while (repeat > 0) {
                digit_offset = digit_offset * 10;
                repeat--;
            }
            out_value += digit_offset;
            position++;
            endrepeat--;
        }
        counter = 0;
        mfc = val[arraysize - 1];
        pulsewidth[mfc] = out_value;
        analogWrite(pin[mfc], pulsewidth[mfc]);
    }
    delay (2);
}


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup() {
    Serial.begin(9600);
    for (int i = 0; i < 6; i++) {
        analogWrite(pin[i], 0);
    }
    Serial.print("");
}
You have not declared the pins to be output pins.
9600 baud is 1/12 the maximum rate that the Arduino can reliably process data. You may not get the throughput needed unless you pick up the pace.

Code:
    while (Serial.available() > 0) {
        delay(1);
        input = Serial.read();
There is at least one character available, so wait a while to read it. I thought you said that speed was a goal. Is it, or not?

Code:
        else if (input >= '0' && input <= '9') {
            input = input - '0';
            val[counter] = input;
            counter++;
        }
Sending string data that needs to be converted back to a number is also not in keeping with speed.

Code:
    if (Serial.available() == 0 && counter > 0) {
How do you know that the data in val[] represents a complete packet?

Code:
    delay (2);
Is speed an issue, or not?
Logged

Pages: [1]   Go Up
Jump to: