Compile error for simple project

Windows 7 Starter (yuck)
HP Mini netbook
Arduino UNO
IDE 0022

All was working well. I started this project with the Potentiometer example from Oomlout:
http://www.oomlout.com/a/products/ardx/circ-08/

I got it to work like it should, and then I started tweaking things. What I'm trying to accomplish is a 4-position software switch for my LED with OFF, LOW, MEDIUM, and HIGH.

int sensorPin = 0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor


void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}


void loop() {
  int thresholdone = 250;
  int thresholdtwo = 500;
  int thresholdthree = 750;
  
{ if(analogRead(sensorPin) < thresholdone);
    {analogWrite(ledPin, 0);}}
    
{  if(analogRead(sensorPin) >= thresholdone; analogRead(sensorPin) < thresholdtwo);
    {analogWrite(ledPin, 25);}}

{  if(analogRead(sensorPin) >= thresholdtwo; analogRead(sensorPin) < thresholdthree);
    {analogWrite(ledPin, 100);}}

{  if(analogRead(sensorPin) >= thresholdthree);
    {analogWrite(ledPin, 256);}}
}

Several changes later, I got the following error messages upon compile:

Twist_A_Potentiometer.cpp: In function 'void loop()':
Twist_A_Potentiometer:46: error: expected )' before ';' token Twist_A_Potentiometer:46: error: expected ;' before ')' token
Twist_A_Potentiometer:49: error: expected )' before ';' token Twist_A_Potentiometer:49: error: expected ;' before ')' token

FWIW, the extra spaces don't seem to make any difference in the other projects, so I use them for neatness. Without the curly braces around each whole "if" phrase I get more errors relating to them. What's my syntax problem?

Thanks!
Patrick

This code won't even work. Here's a representative sample:

{ if(analogRead(sensorPin) >= thresholdthree);
{analogWrite(ledPin, 256);}}

The first problem in the ; after the if(). Removing that:

{ if(analogRead(sensorPin) >= thresholdthree)
{analogWrite(ledPin, 256);}}

That was a big one. Putting a ; there basically said "Check this if statement.. Then do nothing!"

Then removing the extraneous brackets:

You can go with:
if (analogRead(sensorPin) >= thresholdthree)
analogWrite(ledPin, 256);

Or
if (analogRead(sensorPin) >= thresholdthree) {
analogWrite(ledPin, 256);
}

Or my favorite:
if (analogRead(sensorPin) >= thresholdthree)
{
analogWrite(ledPin, 256);
}

Lastly, the range of PWM with analogWrite is 0 to 255. 256 wraps around to 0.

FWIW, the extra spaces don't seem to make any difference in the other projects, so I use them for neatness.

Extra spaces are one thing. You're right. They don't matter. Use them to make the code easier to read.

Extra curly braces are another thing. They do not make the code neater, in my opinion. They, in my opinion, scream "Clueless newbie".

While TeslaFan pointed out several logic issues, they are not the source of the compiler messages. This is:

{  if(analogRead(sensorPin) >= thresholdone; analogRead(sensorPin) < thresholdtwo);
    {analogWrite(ledPin, 25);}}

The function call analogRead returns a value between 0 and 1023. Suppose for a moment that the sensor being read was relatively stable. Both calls to analogRead would return the same value. Suppose that the value was 450. Your statement would resolve to:

{  if(450 >= thresholdone; 450 < thresholdtwo);
    {analogWrite(ledPin, 25);}}

Now, look closely at the conditional part of the if statement. What is 450 >= thresholdone; 450 < thresholdtwo supposed to mean? If you want to have two (or more) conditionals, you need an operator between them, such as && (and) or || (or).

When you decide what the statement is supposed to do, and use the appropriate operator between the conditionals (not :wink: and implement TeslaFans ideas, too. you code should compile.

This is what I love about user forums. People that know what they're doing! :smiley: Thanks for the help. And yes, I'm a screaming newbie, but I've got a big family and a full time job, so this is what I do for me-time.

So, I replaced some of the errant semicolons with &&, and changed 256 to 255, and moved the LED from pin 13 to pin 9, and it compiled, yay! It's doing something now, just not quite what I expected. I hooked up the piezo from the kit to the LED to give me a little more feedback on what it was doing. I'm getting different levels of light/noise, but something's funny. It's like the 0-1-2-3 settings are out of order. Flaky pot, perhaps? I'll check it with my meter shortly.

So far, I've got my LED "lamp" working with Lo, Med, and Hi. What I can't seem to get it to do is turn OFF! I've played with all the variables. All that does is change where in the pot sweep it transitions. I'm puzzled. :~
http://www.oomlout.com/a/products/ardx/circ-08/

We're puzzled too, but at least you can see your code.