Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wrote this sketch to send codes to a speakjet sound processor. It will store any number of three digit integers from 128 to 254. I type them into the serial monitor and hit enter after each one. it is stored in an array, and after 900 is entered, the values stored in the array are sent to the speakjet and also read back to the serial monitor. The issue I'm having is that the array will return a value of three for index 0 every time. I have no clue as to why this is.

Here is the code.

int in=0; 
int code;
int phrase[] {}; 
int x=0;          
String stack=""; 

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    Serial.println("Ready");
}

void loop() {
    while (Serial.available()>0) {
        int inbyte = Serial.read(); 
        if (in<=2);
        {
            stack += (char)inbyte;
            in++;
            //Serial.println(stack);
            //Serial.println(in);
        }

        if (in==3) { 
            if (stack == "900") {
                for(int i=0;i<x;i++) {
                    Serial1.write(phrase[i]);
                    Serial.println(i);
                    Serial.println(phrase[i]);
                    Serial.println();
                }
                stack="";
                x = 0;
                in = 0;
                Serial.println("Ready");
            } else {
                phrase[x]=0;
                int code = stack.toInt();
                if (code < 128) {
                    code = 253;
                }

                phrase[x]=code;
                Serial.println(x);
                Serial.println(phrase[x]);
                x++;
                in=0;

                Serial.write(code);
                Serial.println();

                stack = "";
            }
        }
    }
}

Thanks for any advice, or help with this matter.

Brewer.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

well, first of all, I noticed while reformatting your code you've got a typo here:

    if (in<=2); // <-- if the condition is true, it does nothing
    {
        // this block of code always gets executed
        stack += (char)inbyte;
        in++;
    }

Then I find it really surprising that you can store anything at all in your array:

int phrase[] {}; 

as it is allocated no memory to store something.

I guess those two errors alone can explain a few undefined behaviors for your program. Given those basic things are totally wrong, I did not analyze the validity of your algorithm itself, because it's being late, and I'm about to go to sleep ;-)

share|improve this answer
    
Thanks, I changed the array size from nothing to 30 and the problem is solved. –  Eugene Feb 1 at 2:16
    
As for the typo,(in<=2), it's not. That part of the code works as intended keeping the size of the string down to three characters. It may not be the best way to do that, but it does work. –  Eugene Feb 1 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.