What is wrong with this?
@barrie
That line is not within the scope of a function, so the compiler is saying that t has no idea what n is. You'll need to move the assignment within one of the methods like setup() { ... } or loop() {...} etc. Then the compiler will recognize n as the int as defined.
Anything seems possible when you don't know what you're talking about.
To expand on what @barrie said, the declaration of n, if you intend it to be global, should be before setup. As the code currently exists, steup cannot reference n since it has not been declared. C/C++ creates definitions and allocates variables in the order in which they are encountered.
I don't think that's the problem here. This example places the definition before setup() as you suggest and the compiler still pukes on the assignment.
int n=1; void setup() { // put your setup code here, to run once: } n=2; void loop() { // put your main code here, to run repeatedly: }
Anything seems possible when you don't know what you're talking about.
The attachment was to sort of replicate the problem in my sketch. I declared int n before "setup" in my sketch because I thought that was the birth place of global variables. I tried declaring it in setup but still got the error. @codeslinger are you saying that my declaration above setup should be OK? If so I'll dig deeper and post my sketch if I can't figure it out.
@will are you suggesting I am not doing something silly? I hope so! 🤔
This works ...
int n=1; void setup() { // put your setup code here, to run once: n = 2; } void loop() { // put your main code here, to run repeatedly: }
And note that it's very unlikely that we can help you find a solution if you don't give us an accurate picture of what you're doing.
Anything seems possible when you don't know what you're talking about.
Good, now back to the salt mines !
Anything seems possible when you don't know what you're talking about.
@will By the way your avatar is remarkably like a character in a small animated video I made a long time ago. A funny spoof about advertising. I would show it here but it wouldn't be appropriate.
I chose this one because I always wanted to use something very like it in an advertisement with a slogan like "You'll be stunned by our customer satisfaction level" or something of that ilk.
Also, I have come very late to electronics and so this avatar most closely matches my level of capability and my approach to problem solving 🙂
Anything seems possible when you don't know what you're talking about.
@will I screwed up and referenced the wrong person as the responder before. i should have said that I was expanding on your reply. I was trying to point out that, in addition to doing as you suggested and moving the assignment into a function, that the declaration should of n needs to be made before any functions referencing it. The source of the confusion that @barrie encountered is that the compiler can be rather obtuse with some error messages. This is just one such example.
Not quite. In @barrie's original post, the definition of n DOES precede the use of n, so that is not the problem. The problem is the assignment occurs outside of the scope of any function of subroutine.
Anything seems possible when you don't know what you're talking about.