# If else loop

Hello I have gotten my program to work for the most part. It’s for a 4 wheeled bot that has an onboard PIR and a DHT11 temp/hum sensor controlled via Bluetooth. My motor control has a Left and Right PWM input. I have my code setup where if you press a Capt. F it starts to move forward at a level 55 press again 105 etc. up to 255, but issue is if you press it again it goes to 305 a non-real value output on analogwrite. Below is a copy of my full program any thought on how I can stop it at 255? I am using the Arduino Micro. If you have any thoughts please let me know. Thanks for your time. XD

``````// This version has 2 way comunications and has full foward right left stop and back also has DHT11 module.

// Can read the temp/hum on either Bluetooth phone/LCD on board or Serial Monitor.

double Fahrenheit(double celsius) //Celsius to Fahrenheit conversion
{
return 1.8 * celsius + 32;
}

double Kelvin(double celsius) //Celsius to Kelvin conversion
{
return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078);   // temp var
return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
#include <LiquidCrystal.h>    // Software LCD display
#include <SoftwareSerial.h>   // Software Serial Port
#include <dht11.h>            // Software for Hum/Temp sensor

dht11 DHT11;

LiquidCrystal lcd(A0,A1,A5,A4,A3,A2);
int DHT11PIN = 3;  // extra output
int blpwm = 13;    // Uses pin 13's led to test if device working.
int RxD    = 11;   // Tx from Bluetooth to pin11 Arduino VIOLET WIRE
int lpwm1  = 10;   // pwm left
int rpwm1  = 9;    // pwm right
int pinI1  = 8;    // define I1 interface BROWN WIRE
int pinI4  = 7;    // define I4 interface BLUE WIRE
int pinI3  = 6;    // define I3 interface GREEN WIRE
int pir    = 5 ;   // PIR sensor
int pinI2  = 4;    // define I2 interface YELLO WIRE
int TxD    = 2;    // Rx from Bluetooth to pin 2 Arduino GRAY WIRE
int lpwm   = 5;    // sets lpwm counter start at 30
int rpwm   = 5;    // sets rpwm counter start at 30
int blpwm1 = 10;   // sets blpwm1 counter start at 10
int count  = 5;    // used to set min on start up.
int val    = 0;    // sets val to 0

#define DEBUG_ENABLED  1  // I have no idea.

SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
blueToothSerial.begin(9600);
Serial.begin(9600);       // sets data rate for Serial Monitor
pinMode(pir, INPUT);      // sets pir as a input
pinMode(RxD, INPUT);      // sets RxD as a input
pinMode(TxD, OUTPUT);     // sets TxD as a output
pinMode(pinI4, OUTPUT);   // sets pinI4 as a output
pinMode(pinI3, OUTPUT);   // sets pinI3 as a output
pinMode(pinI2, OUTPUT);   // sets pinI2 as a output
pinMode(pinI1, OUTPUT);   // sets pinI1 as a output
pinMode(rpwm1, OUTPUT);   // sets rpwm1 as a output
pinMode(lpwm1, OUTPUT);   // sets lpwm1 as a output
lcd.begin(16, 2);       // sets lcd type use 16,2 for a 16x2 LCD, etc.
}

void loop()

{                     // Start of main program

char recvChar;

while(1)

{
{
if(Serial.available())    //check if there's any data sent from terminal
{
lcd.print(recvChar);
blueToothSerial.println(recvChar); // Sends data typed on terminal to bluetooth unit.
}
}
if(blueToothSerial.available()) //check if there's any data sent from the remote bluetooth shield
{
lcd.print(recvChar);           // Sends data typed on terminal to lcd display.
if(recvChar == 'B')             // Back
{
lpwm = lpwm + 50;          // increases left speed by 50 each press.
rpwm = rpwm + 50;          // increases right speed by 50 each press.
analogWrite(lpwm1,lpwm);   //
analogWrite(rpwm1,rpwm);   //
digitalWrite(pinI4,HIGH);  // turn DC Motor B move clockwise
digitalWrite(pinI3, LOW);  // turn DC Motor B move clockwise
digitalWrite(pinI2,HIGH);  // turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);   // turn DC Motor A move clockwise
}
else if(recvChar == 'S')   // all stop
{
analogWrite(lpwm1,LOW);    // lpwm off
analogWrite(rpwm1,LOW);    // rpwm off
digitalWrite(pinI4,LOW);   // all stop
digitalWrite(pinI3,LOW);   // all stop
digitalWrite(pinI2,LOW);   // all stop
digitalWrite(pinI1,LOW);   // all stop
digitalWrite(blpwm,LOW);
lpwm = count;                  // re-sets lpwm start at what ever count is at
rpwm = count;                  // re-sets rpwm start at what ever count is at
blpwm1 = 0;                    // re-sets blpwm1 at 0
}
else if (recvChar == 'L')    // turn left
{
lpwm = lpwm + 50;            //  increases left speed by 50 each press.
rpwm = rpwm + 50;            //  increases right speed by 50 each press.
analogWrite(lpwm1, lpwm);
analogWrite(rpwm1, rpwm);
digitalWrite(pinI4,HIGH);
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);
digitalWrite(pinI1,HIGH);
}
else if(recvChar == 'R')     // turn right
{
rpwm = rpwm + 50;              //  increases right speed by 50 each press.
lpwm = lpwm + 50;              //  increases left speed by 50 each press.
analogWrite(rpwm1, rpwm);
analogWrite(lpwm1, lpwm);
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);
digitalWrite(pinI1,LOW);
}
else if(recvChar == 'F')     // FORWARD
{
lpwm = lpwm + 50;
rpwm = rpwm + 50;
analogWrite(lpwm1, lpwm);
analogWrite(rpwm1, rpwm);
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);
digitalWrite(pinI1,HIGH);
}
else if(recvChar == 'I')
{
blueToothSerial.println( );
blueToothSerial.println( );
blueToothSerial.println("Instructions on How to control.");
blueToothSerial.println("Forward                  = F");
blueToothSerial.println("Back                     = B");
blueToothSerial.println("Left                     = L");
blueToothSerial.println("Right                    = R");
blueToothSerial.println("Stop/Light off           = S");
blueToothSerial.println("Temp/Humidity            = W");
blueToothSerial.println("Back Light               = V");

}
else if(recvChar == 'W')                      // Pressing W gets the temp/hum. on LCD/Serial mon and BT.
{
lcd.clear();
switch (chk)
Serial.println();
Serial.println(Fahrenheit(DHT11.temperature), 2);
blueToothSerial.println();
blueToothSerial.println();
lcd.print("Humidity (%): ");
lcd.print((float)DHT11.humidity, 2);
blueToothSerial.print("Humidity      (%): ");
blueToothSerial.println((float)DHT11.humidity, 2);
lcd.setCursor(0,2);
lcd.print("Temp (F): ");
lcd.print(Fahrenheit(DHT11.temperature), 2);
blueToothSerial.print("Temperature   (C): ");
blueToothSerial.println((float)DHT11.temperature, 2);
blueToothSerial.print("Temperature   (F): ");
blueToothSerial.println(Fahrenheit(DHT11.temperature), 2);
blueToothSerial.print("Temperature   (K): ");
blueToothSerial.println(Kelvin(DHT11.temperature), 2);
blueToothSerial.print("Dew Point     (C): ");
blueToothSerial.println(dewPoint(DHT11.temperature, DHT11.humidity));
blueToothSerial.print("Dew PointFast (C): ");
blueToothSerial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
}
else if(recvChar == 'V')            // Back light for lcd
{
blpwm1 = blpwm1 + 5;               // increase back light by 5 each push of V
analogWrite(blpwm,blpwm1);
blueToothSerial.println(blpwm1);
}
else if(recvChar == '+')             // + clears LCD display
{
lcd.clear();                       // Clears LCD
}
}
}
}
``````

Try:

``````     else if(recvChar == 'F')     // FORWARD
{
if (lpwm <= 205 && rpwm <= 205) {
lpwm += 50;
rpwm += 50;
}
analogWrite(lpwm1, lpwm);
analogWrite(rpwm1, rpwm);
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);
digitalWrite(pinI1,HIGH);
}
``````

Now the variables cannot exceed 255. Also, you can make the code more efficient and easier to read by using a switch:

``````   switch (recvChar) {
case 'B':             // ============== Back
lpwm = lpwm + 50;          // increases left speed by 50 each press.
rpwm = rpwm + 50;          // increases right speed by 50 each press.
analogWrite(lpwm1,lpwm);   //
analogWrite(rpwm1,rpwm);   //
digitalWrite(pinI4,HIGH);  // turn DC Motor B move clockwise
digitalWrite(pinI3, LOW);  // turn DC Motor B move clockwise
digitalWrite(pinI2,HIGH);  // turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);   // turn DC Motor A move clockwise
break;
case  'S':   // =======================  stop
analogWrite(lpwm1,LOW);    // lpwm off
analogWrite(rpwm1,LOW);    // rpwm off
digitalWrite(pinI4,LOW);   // all stop
digitalWrite(pinI3,LOW);   // all stop
digitalWrite(pinI2,LOW);   // all stop
digitalWrite(pinI1,LOW);   // all stop
digitalWrite(blpwm,LOW);
lpwm = count;                  // re-sets lpwm start at what ever count is at
rpwm = count;                  // re-sets rpwm start at what ever count is at
blpwm1 = 0;                    // re-sets blpwm1 at 0
break;
case 'L'    // ======================== turn left
default:
Serial.print("We shouldn't be here. recvChar = ");
Serial.println(recvChar, HEX);
break;
}
``````

It’s more efficient because most compilers make a jump table for switch statements. You code may have to fall through a number of false tests before getting to the correct one.

``````if ( lpwm + 50 <= 255 ) {
lpwm = lpwm + 50;
}
if ( rpwm + 50 <= 255 ) {
lpwm = lpwm + 50;
}
``````
``````     while(1)
``````

Why are you running an infinite loop inside an infinite loop()?

Thats a good question didnt see that I had done that till you pointed it out. This is the largest progam i have done yet. The biggest prior was flashing a led on/off. The other idea worked great. Now once you reach 255 the counter pays no attention to anything over 255.

Thanks for the great idea.