if(rSpeed < minSpeed and lSpeed < minSpeed){
analogWrite(pwmPin, pwm);
}
In this specific part of the code the 'and' only reading the part that comes before the 'and' acts like an 'or' . Can anyone explain to me why this is?
Full code:
//User Settings
const int troubleshooting = 1;
const int aggressiveness = 10;
const int minSpeed = 1999; //Increase number to decrease min speed (32000 max)
//Pin Assignment
const int potPin = A0; float pot = 512;
const int mPulseP = 2; int mPulse = 0;
const int lPulseP = 3; int lPulse = 0;
const int rPulseP = 4; int rPulse = 0;
const int pwmPin = 9; int pwm = 127;
const int ledPin = 6;
const int impPin = 7; int imp = 0;
//Millis Value
int millisV;
//500ms Clock
int clock1;
int clockRst1;
//Aggressiveness Clock
int clock2;
int clockRst2;
//Speed Sensors
int mPulseOld; int mSpeed; int mSpeedOld; int m;
int lPulseOld; int lSpeed; int lSpeedOld;
float w;
int rPulseOld; int rSpeed; int rSpeedOld;
int loops;
void setup() {
if (troubleshooting == 1) {
Serial.begin(9600);
}
}
void loop() {
loops++;
//Value Assignment
millisV = millis();
mPulse = digitalRead(mPulseP);
lPulse = digitalRead(lPulseP);
rPulse = digitalRead(rPulseP);
//500ms Clock
clock1 = millisV - clockRst1;
if (clock1 > 500) {
clock1 = 0;
clockRst1 = millisV;
pot = analogRead(potPin);
digitalWrite(ledPin, LOW);
if (pot > 412 and pot < 612) {
pot = 512;
digitalWrite(ledPin, HIGH);
}
pot = pot / 512;
pot = pot / 2;
pot = pot + 0.5;
if (pot > 1) {
pot = pot - 0.1;
}
if (pot < 1) {
pot = pot + 0.1;
}
if (troubleshooting == 1) {
loops = loops * 2;
Serial.print("Loop Speed ~ ");
Serial.print(loops);
Serial.print(" | Motor Speed ~ ");
Serial.print(m);
Serial.print(" | Wheel Speed ~ ");
Serial.print(w);
Serial.print(" | Clock ");
Serial.print(lSpeed);
Serial.print(" | PWM ~ ");
Serial.println(pwm);
}
loops = 0;
}
if (mSpeed <= minSpeed) {
mSpeed = millisV - mSpeedOld;
}
if (mPulse == 1 and mPulseOld != 1) {
mSpeedOld = millisV;
m = mSpeed;
mSpeed = 0;
}
mPulseOld = mPulse;
if (lSpeed <= minSpeed) {
lSpeed = millisV - lSpeedOld;
}
if (lPulse == 1 and lPulseOld != 1) {
lSpeedOld = millisV;
w = lSpeed;
lSpeed = 0;
}
lPulseOld = lPulse;
if (rSpeed <= minSpeed) {
rSpeed = millisV - rSpeedOld;
}
if (rPulse == 1 and rPulseOld != 1) {
rSpeedOld = millisV;
if (rSpeed < w) {
w = rSpeed;
}
rSpeed = 0;
}
rPulseOld = rPulse;
clock2 = millisV - clockRst2;
if (clock2 >= aggressiveness) {
clockRst2 = millisV;
clock2 = 0;
w = w * pot;
if (m > w and pwm < 255) {
pwm++;
}
if (m < w and pwm > 0) {
pwm--;
}
}
if(rSpeed < minSpeed and lSpeed < minSpeed){
analogWrite(pwmPin, pwm);
}
if (rSpeed >= minSpeed and lSpeed >= minSpeed) {
digitalWrite(pwmPin, LOW);
}
imp = digitalRead(impPin);
if (imp = 2) {
digitalWrite(pwmPin, LOW);
}
}
Have you tried,
if( (rSpeed < minSpeed) && (lSpeed < minSpeed) ){
analogWrite(pwmPin, pwm);
}
see Operators in C++?
This looks problematic:
imp = digitalRead(impPin);
if (imp = 2) {
digitalWrite(pwmPin, LOW);
}
Use == for comparison, not =. Also, digitalRead doesn't return two.
When you setup digitalRead(mPulseP);, you know in setup where pin mode can be declared...
Something like pinmode(impIn, INPUT);.
There's nothing wrong with your syntax. Comparison operators take precedence over boolean operators so you don't need parentheses. Also, "and" is an exact synonym for "&&", there is no difference in how they function.
wildbill:
This looks problematic:
imp = digitalRead(impPin);
if (imp = 2) {
digitalWrite(pwmPin, LOW);
}
Use == for comparison, not =. Also, digitalRead doesn't return two.
yeah the single = is a problem but I put a 2 in so that that part of the code wouldn't operate
Idahowalker:
Have you tried,
if( (rSpeed < minSpeed) && (lSpeed < minSpeed) ){
analogWrite(pwmPin, pwm);
}
see [url=https://www.tutorialspoint.com/cplusplus/cpp_operators.htm#:~:text=There%20are%20following%20logical%20operators%20supported%20by%20C%2B%2B,two%20operands%20is%20non-zero%2C%20then%20condition%20becomes%20true.[/url] Operators in C++[/url]?
I did try that and it did the same thing
aarg:
There's nothing wrong with your syntax. Comparison operators take precedence over boolean operators so you don't need parentheses. Also, "and" is an exact synonym for "&&", there is no difference in how they function.
So I guess I'm still at a loss.
I'm very confused as to why it acts as an 'or'. I would have thought it would just do nothing*.*
Serial print these values before the if statement happens rSpeed, minSpeed, lSpeed, and minSpeed.
All your variables that rely on millis() should be unsigned long NOT int.
Steve
yeah the single = is a problem but I put a 2 in so that that part of the code wouldn't operate
That part of the code DOES operate.
"if (imp=2)" is always true, so "digitalWrite(pwmPin, LOW)" is executed.
I fixed it
It was a different part of the code that should have been an or but was an and now it works well
I know I'm not supposed post my full code. I'm not really sure what part of my code is going wrong here, so forgive me not showing the specific area I think is bad.
I want w to equal lSpeed or rSpeed, whichever is higher when a pulse comes through either one, but it usually equals between 0 and 3. It sometimes jumps to the correct number, but not consistently or for long periods of time.
Anyone know why?
Edit: lSpeed and rSpeed are functioning correctly
//User Settings
const int troubleshooting = 1;
const int aggressiveness = 50;
const int minSpeed = 1999; //Increase number to decrease min speed (32000 max)
//Pin Assignment
const int potPin = A0; float pot = 512;
const int mPulseP = 2; int mPulse = 0;
const int lPulseP = 3; int lPulse = 0;
const int rPulseP = 4; int rPulse = 0;
const int pwmPin = 9; int pwm = 0;
const int ledPin = 6;
const int impPin = 7; int imp = 0;
//Millis Value
int millisV;
//500ms Clock
int clock1;
int clockRst1;
//Aggressiveness Clock
int clock2;
int clockRst2;
//Speed Sensors
int mPulseOld; int mSpeed; int mSpeedOld; float m;
int lPulseOld; int lSpeed; int lSpeedOld;
float w;
int rPulseOld; int rSpeed; int rSpeedOld;
long loops;
void setup() {
if (troubleshooting == 1) {
Serial.begin(9600);
}
}
void loop() {
loops++;
//Value Assignment
millisV = millis();
mPulse = digitalRead(mPulseP);
lPulse = digitalRead(lPulseP);
rPulse = digitalRead(rPulseP);
//500ms Clock
clock1 = millisV - clockRst1;
if (clock1 > 500) {
clock1 = 0;
clockRst1 = millisV;
pot = analogRead(potPin);
digitalWrite(ledPin, LOW);
if (pot > 412 and pot < 612) {
pot = 512;
digitalWrite(ledPin, HIGH);
}
pot = pot / 512;
pot = pot / 2;
pot = pot + 0.5;
if (pot > 1) {
pot = pot - 0.1;
}
if (pot < 1) {
pot = pot + 0.1;
}
if (troubleshooting == 1) {
loops = loops * 2;
Serial.print("Loop Speed ~ ");
Serial.print(loops);
Serial.print(" | Motor Speed ~ ");
Serial.print(m);
Serial.print(" | Wheel Speed ~ ");
Serial.print(w);
Serial.print(" | L Clock ");
Serial.print(lSpeed);
Serial.print(" | R Clock");
Serial.print(rSpeed);
Serial.print(" | M Clock ");
Serial.print(mSpeed);
Serial.print(" | PWM ~ ");
Serial.println(pwm);
}
loops = 0;
}
if (mSpeed <= minSpeed) {
mSpeed = millisV - mSpeedOld;
}
if (mPulse == 1 and mPulseOld != 1) {
mSpeedOld = millisV;
m = mSpeed;
mSpeed = 0;
}
mPulseOld = mPulse;
if (lSpeed <= minSpeed) {
lSpeed = millisV - lSpeedOld;
}
if (lPulse == 1 and lPulseOld != 1) {
lSpeedOld = millisV;
w = lSpeed;
lSpeed = 0;
}
lPulseOld = lPulse;
if (rSpeed <= minSpeed) {
rSpeed = millisV - rSpeedOld;
}
if (rPulse == 1 and rPulseOld != 1) {
rSpeedOld = millisV;
if (rSpeed < w) {
w = rSpeed;
}
rSpeed = 0;
}
rPulseOld = rPulse;
imp = digitalRead(impPin);
clock2 = millisV - clockRst2;
if (clock2 >= aggressiveness) {
clockRst2 = millisV;
clock2 = 0;
if (imp == 1) {
w = w * pot;
if (m > w and pwm < 255) {
pwm++;
}
if (m < w and pwm > 0) {
pwm--;
}
}
}
if (mSpeed >= minSpeed) {
m = 2001;
}
if ((lSpeed < minSpeed or rSpeed < minSpeed) and imp == 1) {
analogWrite(pwmPin, pwm);
}
if (lSpeed >= minSpeed) {
if (rSpeed >= minSpeed) {
digitalWrite(pwmPin, LOW);
}
}
if (imp == 0) {
digitalWrite(pwmPin, LOW);
}
if (pwm < 0) {
digitalWrite(pwmPin, LOW);
}
}
I know I'm not supposed post my full code
WRONG !
Please follow the advice on posting code given in posting code
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless
I want w to equal lSpeed or rSpeed, whichever is higher when a pulse comes through either one
if (lSpeed > rSpeed) {
w = lSpeed;
}
else {
w = rSpeed;
}
Sorry, I should have specified that I want w to equal what lSpeed or rSpeed equaled on the last incoming pulse so I don't think I could use this
aarg:
if (lSpeed > rSpeed) {
w = lSpeed;
}
else {
w = rSpeed;
}
Well, your variable names are heavily obfuscated and there are almost no inline comments, so I'm not going to try to figure out how to do that. Maybe you can track those values down and do the same thing with them.
aarg:
Well, your variable names are heavily obfuscated and there are almost no inline comments, so I'm not going to try to figure out how to do that. Maybe you can track those values down and do the same thing with them.
I've done it with this one but for some reason it doesn't work for the other ones even though I've done exactly the same thing
if (mSpeed <= minSpeed) {
mSpeed = millisV - mSpeedOld;
}
if (mPulse == 1 and mPulseOld != 1) {
mSpeedOld = millisV;
m = mSpeed;
mSpeed = 0;
}
This "what"? What "other ones"? What did you do? You haven't clarified anything about what those code segments are supposed to do, or how they do it. I suggest renaming your variables so they are meaningful, adding comments about what is going on, and repost. Before you do that, please read reply #1.
One thing that will cause you big problems
//Millis Value
int millisV;
millis() returns an unsigned long, not an int. An int will overflow after 32767mS, which will probably cause all sorts of problems in other parts of your code.
One thing that will cause you big problems
Already pointed out in the cross post (reported).
@confusedpotato34
TOPIC MERGED.
Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.