Where'd my data go? bluetooth

So i have taken a very basic concept, the ol' android slider to servo program and re appropriated it to perform a three different but basic functions. I began testing and got almost nowhere. When I uncomment the line "Serial.println(val);" in the program and run the Serial Monitor I get exactly the values displayed on the Monitor that I'm expecting to see, however when I put a "Serial.println(val);" in the very next function, a simple "if" to grab a range and do something with it, I get nothing. As well as any other part of the program. Since the program is also not performing the expected actions I am assuming that the data is not getting where I need it. But where is it going? Thanks for any help.

int pinMotor1Plus = 2; //motor #1 +
int pinMotorNeg = 3; //motor #1 -
const byte pinMotor2PWM = 9; //motor 2 pwm
const byte pinMotor3Plus = 4; //motor #3 +
const byte pinMotor3Neg = 5; //motor #3 -

int analogPin1 = A0; //read location for bypass valve pot
int analogPin2 = A1; //read location for flapper valve pot
int analogPin3 = A2; //read location for temp sensor

unsigned long previousMillis = 0;
unsigned long interval = 2000;


void setup() {
  Serial.begin(9600);

  pinMode(pinMotor1Plus, OUTPUT);
  pinMode(pinMotorNeg, OUTPUT);
  pinMode(pinMotor3Plus, OUTPUT);
  pinMode(pinMotor3Neg, OUTPUT);
  pinMode(pinMotor2PWM, OUTPUT);

  digitalWrite(pinMotor1Plus, LOW);
  digitalWrite(pinMotorNeg, LOW);
  digitalWrite(pinMotor3Plus, LOW);
  digitalWrite(pinMotor3Neg, LOW);
  analogWrite(pinMotor2PWM, 50);


}

void loop() {
  // put your main code here, to run repeatedly:

  float refvoltage; //comparison value for reference signals...provided by arduino
  int temprequest; //requested change from android app
  int fanSpeed; //calculated value based on val
  int value; //another calculated value based on val, for a different purpose
  int potvoltage1; //input from motor1 position
  int potvoltage2; //input from motor3 position
  int val; //bluetooth data in

  int tempF; //temperature converted to Fahrenheit
  float tempS;//value from temp sensor
  float tempC;//calulated value of temp sensor in Celsius


  if (Serial.available() >= 2 ) {
    unsigned int a = Serial.read();
    unsigned int b = Serial.read();
    unsigned int val = (b * 256) + a;
    //Serial.println(val);
  }
  //*******************200 to 300*******************
  if (val > 200 && val < 300) { //requested change in fan speed
    Serial.println(val);
    fanSpeed = ((val - 200) * 12.5);
    analogWrite(pinMotor2PWM, fanSpeed);
  }

  //*******************300 to 400********************
  else if (val > 300 && val < 400) { //requested new flapper zone

    value = ((val - 300) * 261);
    Serial.println(value)  ;
    potvoltage2 = analogRead(analogPin2);
    if (value < potvoltage2) {
      digitalWrite(pinMotor3Plus, HIGH);
      digitalWrite(pinMotor3Neg, LOW);
      while (value < analogRead(analogPin2));
    }
    else if (value > potvoltage2)
    {
      Serial.println(value);
      digitalWrite(pinMotor3Plus, LOW);
      digitalWrite(pinMotor3Neg, HIGH);
      while (value > analogRead(analogPin2));
    }
  }

  //*****************100 to 200*******************
  else if (val > 100 && val < 200) {// change in temperature setting
    potvoltage1 = analogRead(analogPin1);
    int refvoltage = analogRead(analogPin1);
    tempS = analogRead(analogPin3);
    tempC = (tempS * .48);
    tempF = (tempC * 1.8) + 32;
    Serial.println(tempF);
    temprequest = (val - 100);
    if (val == 164 ) {
      while (potvoltage1 > 41 ) { //where 41 = bypass valve closed pot signal voltage
        digitalWrite(pinMotor1Plus, HIGH);
        digitalWrite(pinMotorNeg, LOW);
        potvoltage1 = analogRead(analogPin1);
      }
    }
    else if (val == 186) {
      while (potvoltage1 < 977 ) { //where potvoltage is the signal voltage from the bypass valve and 977 = bypass valve open pot signal voltage
        digitalWrite(pinMotorNeg, HIGH);
        digitalWrite(pinMotor1Plus, LOW);
        potvoltage1 = analogRead(analogPin1);
      }
    }
    else if (temprequest > 64 && temprequest < 86 ) { //begin loop to move valve aperture
      while (tempF < temprequest) {//temperature requested is hotter than current temp
        refvoltage = (potvoltage1 + 40);
        while (refvoltage > potvoltage1) {
          unsigned long currentMillis = millis();
          if ((currentMillis - previousMillis) < interval) {
            previousMillis = currentMillis;
            refvoltage = (refvoltage + 40); //where refvoltage is the reference voltage for the next segment (1/24th)
            digitalWrite(pinMotorNeg, HIGH);
            digitalWrite(pinMotor1Plus, LOW);
          }
        }
      }
      while (tempF > temprequest) { // temperature requested is cooler than current temp
        while (refvoltage < potvoltage1) {
          refvoltage = (potvoltage1 - 40);
          unsigned long currentMillis = millis();
          if ((currentMillis - previousMillis) < interval) {
            previousMillis = currentMillis;
            refvoltage = (refvoltage - 40); //moving down the scale .2V at a time
            digitalWrite(pinMotor1Plus, HIGH);
            digitalWrite(pinMotorNeg, LOW);
          }
        }
      }
    }



  }

}

Sounds like "val" isn't the right value to fire the if statement.

Either way, your description is confusing and lacks detail. What exactly are you trying to do? What is your project description? Can you give example data and how the program is reacting to it wrong?

I also noticed that you have a variable "val" and a variable "value". Are you sure you aren't mixing them up?

if you scroll down to where it says "//***********200 to 300 *************" the block of code above is the Serial.available function. In that block of code you will see the "//Serial.println(val);" I have placed there for troubleshooting. If I uncomment that statement then I get the expected values on the Serial Monitor, that is 100 to 200 for slider 1, 200 to 300 for slider 2, and 300 to 400 for slider 3. This indicates that this portion of the code is working fine. In the very next block of code (200 to 300) you will see the same "Serial.println(val);". Which should print the value of (val) to the Serial Monitor as long as it is in between 200 and 300 but it does not. I don't see how the purpose of the project is important but if it will help, I'm trying to use the arduino to control the fan (pinMotor2PWM), the zone motor (pinMotor3), and the coolant bypass valve (pinMotor1) which controls the flow of coolant from the engine into the passenger compartment to regulate the temperature of the air in the cab. I feel like you're trying to read into too much, which, if that's your process great. But really I'm emulating every basic android slider to servo program I've ever seen only I've made it slightly more complicated. It's most likely something very simple and basic I'm overlooking here, but I've been staring at it so long I can't see it. Thanks so much for your help though.

This variable named val

    unsigned int val = (b * 256) + a;

is not the same variable as this one

 int val; //bluetooth data in

even though they have the same name.

Some research on the scope of variables would seem to be needed

Change line 53 from

    unsigned int val = (b * 256) + a;

to

    val = (b * 256) + a;

and see what happens

...R

Why is it people on this website always just assume you don't know what you're doing because you make a simple mistake? Ya know sometimes you stare at the same thing for so long, especially when it's your work, you just can't see the faults. That's why you ask for help. I am confident your suggestion will help greatly though, thank you so much.

Can I assume from your post that you understand the problem and how to fix it ?

scott_funkhouser:
Ya know sometimes you stare at the same thing for so long, especially when it's your work, you just can't see the faults.

We all do that.

Why are you assuming that we "assume you don't know what you're doing"

...R

Yep that was it. I'm getting into each subroutine now as a println for fanSpeed, potvoltage2, and tempF, in the appropriate locations all yield the expected output on the Serial monitor. There's still work to do but I'm not awful at troubleshooting. Thanks so much guysguys. Merry Christmas!

I think it was mostly the reference to a "study of the scope of variables." That made me feel condescended to. I took your direction though and it was greatly helpful and appreciated.

scott_funkhouser:
I think it was mostly the reference to a "study of the scope of variables." That made me feel condescended to.

You must keep in mind that very many of the people asking questions here do not know about scope (or other C++ basics) and it makes more sense to mention it up front rather than having to do so in a later reply.

...R

I know what I learned from a few hours reading on this website and watching a few tutorials. I learn very quickly and I have a necessity that drives me: heat in my truck. I just think you put yourself at a disadvantage when you jump to conclusions. If a person asks you for help say you're not willing, not able, or just lend a hand. I was looking for some specific help, and you made a specific suggestion. I'm just saying I could've done without the ensuing condescension. But, as I said previously, I really appreciate the helpful suggestion.

scott_funkhouser:
I'm just saying I could've done without the ensuing condescension.

I don't believe anyone attempted to condescend to you.

...R

I just think you put yourself at a disadvantage when you jump to conclusions.

My conclusion from seeing your code was that declaring two variables with the same name but different scope was causing the problem.

My suggestion that you do some research on scope would, I believe, have one of two outcomes. Either you would research scope, understand the problem and fix it or, if you already knew about scope and had just made a silly error and you would understand it and fix it.

Ok. Maybe I overreacted or possibly read something into your words that you hadn't intended. O apologize. Your suggestion, of course, fixed the issue with val. In the 200 to 300 routine a println of val yields the expected result. A printin of fanSpeed also displays exactly the expected data. In the 300 to 400 routine, in either the if, or else if, a println of val does nothing. Please help, it's 17° F here and I have no heater. I have all of the necessary hardware to make this work I'm just lacking something in my code to make it run. And, I somehow fried another arduino. The computer recognizes it but won't allow me to upload sketches so I can't even test run for another 3 days until a replacement arrives.

Please post the latest version of your program.

The code I had after you pointed out my error about the variable (val) is posted below. You will notice I have 4 Serial.println commands commented out. I was using these to figure out where my program was going wrong when something happened and I can no longer upload code to my arduino. The Serial.println in the Serial.available routine works as it should when it is uncommented, as does the Serial.println(fanSpeed) in the 200-300 subroutine. The two commented Serial.println in the 300-400 and 100-200 correctly print their respective values 1 time only. The companion app is available in the MIT AppInventor2 Gallery as "Carclimatecontrol_copy"

This is the second one I've burned out so I must be doing something wrong. The computer makes that sound when I plug it in but when I try to upload it says :

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x79
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x79
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x79
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x79
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x79
avrdude: stk500_recv(): programmer is not responding

Sure wish I knew what caused it. Anyways, here's the code. I will answer all questions.

int pinMotor1Plus = 2; //motor #1 +
int pinMotorNeg = 3; //motor #1 -
const byte pinMotor2PWM = 9; //motor 2 pwm
const byte pinMotor3Plus = 4; //motor #3 +
const byte pinMotor3Neg = 5; //motor #3 -

int analogPin1 = A0; //read location for bypass valve pot
int analogPin2 = A1; //read location for flapper valve pot
int analogPin3 = A2; //read location for temp sensor

unsigned long previousMillis = 0;
unsigned long interval = 2000;


void setup() {
  Serial.begin(9600);

  pinMode(pinMotor1Plus, OUTPUT);
  pinMode(pinMotorNeg, OUTPUT);
  pinMode(pinMotor3Plus, OUTPUT);
  pinMode(pinMotor3Neg, OUTPUT);
  pinMode(pinMotor2PWM, OUTPUT);

  digitalWrite(pinMotor1Plus, LOW);
  digitalWrite(pinMotorNeg, LOW);
  digitalWrite(pinMotor3Plus, LOW);
  digitalWrite(pinMotor3Neg, LOW);
  analogWrite(pinMotor2PWM, 50);


}

void loop() {
  // put your main code here, to run repeatedly:

  float refvoltage; //comparison value for reference signals...provided by arduino
  int temprequest; //requested change from android app
  int fanSpeed; //calculated value based on val
  int value; //another calculated value based on val, for a different purpose
  int potvoltage1; //input from motor1 position
  int potvoltage2; //input from motor3 position
  int val; //bluetooth data in

  int tempF; //temperature converted to Fahrenheit
  float tempS;//value from temp sensor
  float tempC;//calulated value of temp sensor in Celsius


  if (Serial.available() >= 2 ) {
    unsigned int a = Serial.read();
    unsigned int b = Serial.read();
    val = (b * 256) + a;
    //Serial.println(val);
  }
  //*******************200 to 300*******************
  if (val > 200 && val < 300) { //requested change in fan speed
    fanSpeed = ((val - 200) * 12.5);
    //Serial.println(fanSpeed);
    analogWrite(pinMotor2PWM, fanSpeed);
  }

  //*******************300 to 400********************
  else if (val > 300 && val < 400) { //requested new flapper zone

    value = ((val - 300) * 261);
    potvoltage2 = analogRead(analogPin2);
    //Serial.println(potvoltage2);
    if (value < potvoltage2) {
      while (value < analogRead(analogPin2));
      digitalWrite(pinMotor3Plus, HIGH);
      digitalWrite(pinMotor3Neg, LOW);
    }
    else if (value > potvoltage2) {
      while (value > analogRead(analogPin2));
      Serial.println(potvoltage2);
      digitalWrite(pinMotor3Plus, LOW);
      digitalWrite(pinMotor3Neg, HIGH);
    }
  }

  //*****************100 to 200*******************
  else if (val > 100 && val < 200) {// change in temperature setting
    potvoltage1 = analogRead(analogPin1);
    int refvoltage = analogRead(analogPin1);
    tempS = analogRead(analogPin3);
    tempC = (tempS * .48);
    tempF = (tempC * 1.8) + 32;
    //Serial.println(tempF);
    temprequest = (val - 100);
    if (val == 164 ) {
      while (potvoltage1 > 41 ) { //where 41 = bypass valve closed pot signal voltage
        digitalWrite(pinMotor1Plus, HIGH);
        digitalWrite(pinMotorNeg, LOW);
        potvoltage1 = analogRead(analogPin1);
      }
    }
    else if (val == 186) {
      while (potvoltage1 < 977 ) { //where potvoltage is the signal voltage from the bypass valve and 977 = bypass valve open pot signal voltage
        digitalWrite(pinMotorNeg, HIGH);
        digitalWrite(pinMotor1Plus, LOW);
        potvoltage1 = analogRead(analogPin1);
      }
    }
    else if (temprequest > 64 && temprequest < 86 ) { //begin loop to move valve aperture
      while (tempF < temprequest) {//temperature requested is hotter than current temp
        refvoltage = (potvoltage1 + 40);
        while (refvoltage > potvoltage1) {
          unsigned long currentMillis = millis();
          if ((currentMillis - previousMillis) < interval) {
            previousMillis = currentMillis;
            refvoltage = (refvoltage + 40); //where refvoltage is the reference voltage for the next segment (1/24th)
            digitalWrite(pinMotorNeg, HIGH);
            digitalWrite(pinMotor1Plus, LOW);
          }
        }
      }
      while (tempF > temprequest) { // temperature requested is cooler than current temp
        while (refvoltage < potvoltage1) {
          refvoltage = (potvoltage1 - 40);
          unsigned long currentMillis = millis();
          if ((currentMillis - previousMillis) < interval) {
            previousMillis = currentMillis;
            refvoltage = (refvoltage - 40); //moving down the scale .2V at a time
            digitalWrite(pinMotor1Plus, HIGH);
            digitalWrite(pinMotorNeg, LOW);
          }
        }
      }
    }



  }

}