Hi, i cant figure out how to do an if statement in my while loop i searched the forum, google etc but my question still issn't answerd.
The thing i want with the if statement is when one of the 3 values of the potentiometer reaches 255 (the values ar gotten from the I2c) a led will light up, i tried some things but couldnt get it to compile.
This is the whole code.
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}[code]
This is the part I need the if statement in.
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}[/code]
I hope for some tips and how to approach this issue.
Ok...... two things here. The first thing is .... you mentioned, you couldn't get the code to compile. That's one thing. What kind of error messages etc did you see when you tried to compile?
Then, the other thing is..... the 'if' statement. What you want to do is to have three values ready for you to compare. Eg, A, B and C. So if any of these values equals what you said (ie. 255), then light something up...
eg. if ( (A == 255) || (B == 255) || (C == 255) ) {
digitalWrite(some_pin_number_or_label, HIGH);
}
eg. if ( (A == 255) || (B == 255) || (C == 255) ) {
digitalWrite(ledPin, HIGH);
}
Also, for that pin number that is meant to light up and LED.... define that pin somewhere BEFORE void setup()........such as:
Southpark:
Ok...... two things here. The first thing is .... you mentioned, you couldn't get the code to compile. That's one thing. What kind of error messages etc did you see when you tried to compile?
Then, the other thing is..... the 'if' statement. What you want to do is to have three values ready for you to compare. Eg, A, B and C. So if any of these values equals what you said (ie. 255), then light something up...
eg. if ( (A == 255) || (B == 255) || (C == 255) ) {
digitalWrite(some_pin_number_or_label, HIGH);
}
eg. if ( (A == 255) || (B == 255) || (C == 255) ) {
digitalWrite(ledPin, HIGH);
}
Also, for that pin number that is meant to light up and LED.... define that pin somewhere BEFORE void setup()........such as:
int ledPin = 13;
When i tried my idea's it couldnt compile this is how i could get with the code, and the error messages looked like:''exit status 1 expected primary-expression before '}' token''
And thanks for the reaction going to try this never knew you could write it down like this ''if ( (A == 255) || (B == 255) || (C == 255) ) '' im new to coding. it's for my internship im doing now.
wvmarle:
Error message suggests you have a } too many.
And yes, you can also write it like that.
haha never knew that thanks for that, do you know any site or things to search for so i know the error codes better and know what they mean? Cause just googeling the error doesnt work for me they are a bit diffrent and the solutions dont work for me then.
The solution in this case: scour your code and find the extraneous } which can be lots of lines before where the error is thrown, literally hundreds of lines, this kind of errors can be hard to find. Doing a Ctrl-T helps a lot, it fixes your indentation and that in turn helps you to find where the offending block is.
To understand error messages, make sure you understand C++ syntax, as in fact the error message does tell you pretty much what's wrong: something with the } token, and you should know you must have the { and } tokens balanced.
wvmarle:
The solution in this case: scour your code and find the extraneous } which can be lots of lines before where the error is thrown, literally hundreds of lines, this kind of errors can be hard to find. Doing a Ctrl-T helps a lot, it fixes your indentation and that in turn helps you to find where the offending block is.
To understand error messages, make sure you understand C++ syntax, as in fact the error message does tell you pretty much what's wrong: something with the } token, and you should know you must have the { and } tokens balanced.
it was hard to find but i found it, i knew it had to be balanced but then again i didn't get the error code. Thats why i asked if you knew a site, i did diffrent courses on tutorialspoint.com about c++ from beginner and then steps up that helped alot back then but the error codes weren't included in there
unfortunately.
AngelinaNoLee:
haha never knew that thanks for that, do you know any site or things to search for so i know the error codes better and know what they mean? Cause just googeling the error doesnt work for me they are a bit diffrent and the solutions dont work for me then.
I have seen in the 'arduino sketch' (IDE software) that putting the mouse cursor onto the first curly bracket {
will highlight the OTHER curly bracket in some other colour. This allows people to check the locations of a curly bracket and its matching one. If anything looks out of place when doing these checks, then ..... as mythbusters say 'well, there's ur PRarrblem'.
Southpark:
I have seen in the 'arduino sketch' (IDE software) that putting the mouse cursor onto the first curly bracket {
will highlight the OTHER curly bracket in some other colour. This allows people to check the locations of a curly bracket and its matching one. If anything looks out of place when doing these checks, then ..... as mythbusters say 'well, there's ur PRarrblem'.
Thanks for the tip going to try that in the future.
[qoute]Error messages are also compiler specific, not just language specific.[/qoute]
We are on the arduino forum so everyone here use the same compiler right? Or are there more compilers for the arduino sketches?
This is the slave code now, but when i reaceive 255 on x (x = Wire.read() ) it doesnt light up the led. In the master code i only send one potentiometer value for testing, how come the led doesnt light up if im not mistaken my statements are right.
int LED = 46;
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
pinMode(LED, OUTPUT);
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
if (Wire.read() == 255) {
digitalWrite(LED, HIGH);
}
}
Error messages are also compiler specific, not just language specific.
We are on the arduino forum so everyone here use the same compiler right? Or are there more compilers for the arduino sketches?
I wouldn't be so sure about that.
For me I guess that'd be gcc as that's by default installed on Linux computers. If you're running another flavour of Linux or maybe a BSD, you may have another version of gcc. If you're on Apple, or even Windows, fair chance you have a different compiler.
We may all be using the same IDE - well, partly, as a large part of what I do is library code at the moment and the IDE doesn't support .cpp or .h files, so doing that in xed, my text editor, then the IDE to run the compilation and take care of the upload - even same IDE doesn't mean same compiler. That are two different things.
slipstick:
You read x and print it. Then you read another character and check if that is 255 to switch the LED on.
Steve
but x== Wire.read so its the same then i still changed it to x and doesnt work
wvmarle:
We are on the arduino forum so everyone here use the same compiler right? Or are there more compilers for the arduino sketches?
I wouldn't be so sure about that.
For me I guess that'd be gcc as that's by default installed on Linux computers. If you're running another flavour of Linux or maybe a BSD, you may have another version of gcc. If you're on Apple, or even Windows, fair chance you have a different compiler.
We may all be using the same IDE - well, partly, as a large part of what I do is library code at the moment and the IDE doesn't support .cpp or .h files, so doing that in xed, my text editor, then the IDE to run the compilation and take care of the upload - even same IDE doesn't mean same compiler. That are two different things.
hmm didn't know that thought every ide compiler was the same, thanks for the info.
Don't have an answer for you here , just a suggestion that may help.I was having many issues with code understanding and found a site that really helped me .Top Tech Boy has online videos that help understand some of these issues ,hope this helps somewhat.
Larryfos:
Don't have an answer for you here , just a suggestion that may help.I was having many issues with code understanding and found a site that really helped me .Top Tech Boy has online videos that help understand some of these issues ,hope this helps somewhat.