"error: expected primary-expression before '}' token" is the message I keep receiving, this is the part of my code, I believe is relevant:
const int sensorA = 5;
const int sensorB = 6;
void setup() {
// put your setup code here, to run once:
pinMode(cwRot, OUTPUT);
pinMode(cwRot, OUTPUT);
pinMode(sensorA, INPUT);
pinMode(sensorB, INPUT);
}
void loop() {
digitalWrite(cwRot, HIGH);
digitalRead(sensorA);
digitalRead(sensorB);
if(sensorA, HIGH && sensorA,LOW){
digitalWrite(cwRot, LOW);
while(i != numberCyRoT){
digitalWrite(cwRot, HIGH);
digitalRead(sensorA);
digitalRead(sensorB);
if(sensorB,HIGH && sensorA,LOW){
i =+
}
}
}
}
Usually, I would put the "}"-brackets at the end of each logic statement, but I am trying incorporate all of the loops into one big loop with small feedbacks. I have counted the bracket pairs, and there are 4 start-brackets and 4 end-brackts.
- Also, quick question, is my formatting of the if(sensorB, HIGH && sensorA,LOW) correct? Or is it if(sensorB = HIGH && sensorA = LOW)?
Thank you for taking your time reading this.
Please use code tags.
You put:
if(sensorB,HIGH && sensorA,LOW){
i =+
}
It should be:
if(sensorB == HIGH && sensorA == LOW){
i =+; // This won't do anything anyway.
}
However, as your sensors are not defined as bool none of that will work as you expect.
I assume these are two ways of writing the if-statement? Or is the latter the only correct one?
Also, do you have any idea, why I am getting the error message?
I assume these are two ways of writing the if-statement? Or is the latter the only correct one?
The first is your way, which is wrong. The second is correct.
Also, do you have any idea, why I am getting the error message?
Yes, because your if statement is wrong, and because you missed the ; from the i +=.
That's not the only problem, your 2 sensor values never change as they are defined const, so there is no point in testing them, and in any case, as they are not bool they cannot be HIGH or LOW.
pinMode(cwRot, OUTPUT);
pinMode(cwRot, OUTPUT);
Why twice? Where is the cwRot variable declared?
while (i != numberCyRoT)
Where is the variable i declared? Where is the variable numberCyRoT declared?
if(sensorB = HIGH && sensorA = LOW)
Is correct.
Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.
Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
Code: [Select]
if(sensorB = HIGH && sensorA = LOW)
Is correct.
I think you mean
if(sensorB == HIGH && sensorA == LOW)
I think you mean
Yes, Thanks. I don't know why I did not catch that. Forrest for the trees, I guess.
Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
Oh alright, I can do that. Just thought it would be more efficient the other way.
Error:
Arduino: 1.8.11 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
C:\Users\Mandem\Documents\Arduino\Rotary_encoder\Rotary_encoder.ino: In function 'void loop()':
Rotary_encoder:35:14: error: expected primary-expression before ';' token
i += ;
^
exit status 1
expected primary-expression before ';' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Here is my full code:
//Calculations
int angle = 2; // Angle you wish
int numberCyRo = 10; //Number of cycles per 360 degrees
int numberCyRoT = angle / 360 * numberCyRo;
//standalone variables
int i = 0 -1;
//pins
const int cwRot = 3;
const int ccwRot = 4;
const int sensorA = 5;
const int sensorB = 6;
void setup() {
// put your setup code here, to run once:
pinMode(cwRot, OUTPUT);
pinMode(ccwRot, OUTPUT);
pinMode(sensorA, INPUT);
pinMode(sensorB, INPUT);
} void loop() {
digitalWrite(cwRot, HIGH);
digitalRead(sensorA);
digitalRead(sensorB);
if (sensorA == HIGH && sensorA == LOW) {
digitalWrite(cwRot, LOW);
while (i != numberCyRoT) {
digitalWrite(cwRot, HIGH);
digitalRead(sensorA);
digitalRead(sensorB);
if (sensorB == HIGH && sensorA == LOW) {
i +=;
}
}
}
}
Also, why would non-boolean logic not work?
digitalRead(sensorA);
Aren't you interested in the result?
TheMemberFormerlyKnownAsAWOL:
digitalRead(sensorA);
Aren't you interested in the result?
What do you mean?
I want it so, that when A is low, and B is high, it will count a cycle towards the target amount of cycles - 1.
I mean "what's the point of a digitalRead if you don't assign the result to a variable, or directly test the return value?"
TheMemberFormerlyKnownAsAWOL:
I mean "what's the point of a digitalRead if you don't assign the result to a variable, or directly test the return value?"
Ah.... I see your point. Fixed it, thank you.
Also, why would non-boolean logic not work?
In your code you have:
const int sensorA = 5;
const int sensorB = 6;
So sensorA is 5 and always 5 and never anything else, and SensorB is 6 and always 6 and never anything else. There's no point in testing them if they never change, and they are ints, not bool, so they can't be just HIGH or LOW, ints can have lots of different values from -32,768 to 32,767, not just HIGH and LOW. If you want HIGH and LOW you need bool.
Ah.... I see your point. Fixed it, thank you.
Please show us your new code...
I strongly suspect that what you actually want is this:
if (digitalRead(sensorB) == HIGH && digitalRead(sensorA) == LOW {
i +=; // This still does nothing....
}
PerryBebbington:
Please show us your new code...
I strongly suspect that what you actually want is this:
if (digitalRead(sensorB) == HIGH && digitalRead(sensorA) == LOW {
i +=; // This still does nothing....
}
Here is my new code:
//pins
const int cwRot = 3;
const int ccwRot = 4;
const int sensorA = 5;
const int sensorB = 6;
//Calculations
int angle = 2; // Angle you wish
int numberCyRo = 11; //Number of cycles per 360 degrees
int numberCyRoT = angle / 360 * numberCyRo;
//standalone variables
int i = 0;
int digA = digitalRead(sensorA);
int digB = digitalRead(sensorB);
void setup() {
// put your setup code here, to run once:
pinMode(cwRot, OUTPUT);
pinMode(ccwRot, OUTPUT);
pinMode(sensorA, INPUT);
pinMode(sensorB, INPUT);
} void loop() {
if (digB != HIGH && digA != LOW) {
digitalWrite(cwRot, HIGH);
if (digB == HIGH && digA == LOW) {
digitalWrite(cwRot, LOW);
while (i != numberCyRoT) {
digitalWrite(cwRot, HIGH);
digitalRead(sensorA);
digitalRead(sensorB);
if (digB == HIGH && digA == LOW) {
int i = i + 1 ;
}
}
}
}
}
And why do you suppose the counting with the variable i won't work?
int i = i + 1 ;
There's no way that's going to work.
if (digB == HIGH && digA == LOW) {
int i = i + 1 ; // creates a (new) local variable i that only exists in this for loop.
// The global i variable is not affected.
}
Lose the int before i to increment the global variable i.
You are not reading your inputs anywhere inside loop()...