counter program

Hi folks,
new to this Arduino stuff and struggling with something. Im trying to create a sketch following this flowchart:

this is my sketch:

int switchPin = 2;
int buzzerPin = 7;
int ovenPin = 13;

void setup()
{
pinMode(switchPin, INPUT); // initialize the switch pin as an output
pinMode(buzzerPin, OUTPUT); // initialize the Buzzer pin as an input
pinMode(ovenPin, OUTPUT); // initialize the Magnetron pin as an input
}

void loop()
{
if (digitalRead(switchPin) == HIGH)
digitalWrite(ovenPin, HIGH);
delay (10000)
digitalWrite(ovenPin, LOW);
}
{ for (int counter = 1 ; counter <= 5; counter = counter +1)
{ digitalWrite(buzzerPin, HIGH);
delay (200);
digitalWrite(buzzerPin, LOW);
delay (100);}
}

else
{
digitalWrite( ledPin, LOW);
}
}

My errors are:
sketch_jun16a.ino: In function ‘void loop()’:
sketch_jun16a:17: error: expected `;’ before ‘digitalWrite’
sketch_jun16a.ino: At global scope:
sketch_jun16a:19: error: expected unqualified-id before ‘{’ token

Not really sure what these mean so any help would be really appreciated!!!

Ps I know you can calibrate the button, but not doing that just yet - I’m creating a scheme for work for 15 year old Engineering Science pupils and trying to get them understand the basics first

sketch_jun16a.ino: In function 'void loop()':
sketch_jun16a:17: error: expected `;' before 'digitalWrite'
sketch_jun16a.ino: At global scope:
sketch_jun16a:19: error: expected unqualified-id before '{' token

Put a ; after above line
You have transposed { with (

You also need a semicolon after your delay(...)

else
  {
  digitalWrite( ledPin, LOW);
  }

ledPin isn't declared. It's not clear from your flowchart what you're trying to do at this point.

wildbill:

else

{
  digitalWrite( ledPin, LOW);
  }




ledPin isn't declared. It's not clear from your flowchart what you're trying to do at this point.

yeah that was wrong sorry (it was copied and pasted from another code to help me try work stuff out)
i meant to put:

else
{
digitalWrite(buzzerPin, LOW);
digitalWrite(ovenPin, LOW);
}
}

Your { and } seem a bit out of synch. Here’s a tip: in the IDE, place your cursor just after any ( or ) or { or }, the IDE puts a little box around the corresponding one in the pair, so you can keep track of them.

But… you really ought to look at the Blink Without Delay method as explained here. Further info here and here.

steinie44:

sketch_jun16a.ino: In function 'void loop()':
sketch_jun16a:17: error: expected `;' before 'digitalWrite'
sketch_jun16a.ino: At global scope:
sketch_jun16a:19: error: expected unqualified-id before '{' token

Put a ; after above line
You have transposed { with (

Could I ask a MASSIVE favour and tell me what line and where I've used the wrong bracket by changing the code and pasting it on here? I'm really sorry, but I'm really rubbish with this - feel free to talk me to like a child haha

Ive managed to sort some of them (hopefully) but now Im getting the errors:

sketch_jun16a.ino: In function ‘void loop()’:
sketch_jun16a:26: error: ‘else’ without a previous ‘if’

the first one is the same probem I had, the second one though im confused about as I have the if previously and is inside the needed brackets. Modified sketch is:

int switchPin = 2;
int buzzerPin = 7;
int ovenPin = 13;

void setup()
{
pinMode(switchPin, INPUT); // initialize the switch pin as an output
pinMode(buzzerPin, OUTPUT); // initialize the Buzzer pin as an input
pinMode(ovenPin, OUTPUT); // initialize the Magnetron pin as an input
}

void loop()
{
if (digitalRead(switchPin) == HIGH)
{digitalWrite(ovenPin, HIGH);
delay (10000);
digitalWrite(ovenPin, LOW);
}
{ for (int counter = 1 ; counter <= 5; counter = counter +1)
{ digitalWrite(buzzerPin, HIGH);
delay (200);
digitalWrite(buzzerPin, LOW);
delay (100);
}
}
else
{
digitalWrite(buzzerPin, LOW);
digitalWrite(ovenPin, LOW);
}
}

once again any help you can offer is greatly appreciated!!

When you compile the sketch. The error will be highlighted also a line number will be shown.

sketch_jun16a:17: error: expected `;’ before ‘digitalWrite’

The 17 is the line number. It won’t always show the exact line, but it will be close.

steinie44:
When you compile the sketch. The error will be highlighted also a line number will be shown.

sketch_jun16a:17: error: expected `;' before 'digitalWrite'

The 17 is the line number. It won't always show the exact line, but it will be close.

ah did not know that - thank you! :smiley: still cant see what im doing wrong though haha :blush:

int switchPin = 2;
int buzzerPin = 7;
int ovenPin = 13;

void setup()
{
  pinMode(switchPin, INPUT);    // initialize the switch pin as an output
  pinMode(buzzerPin, OUTPUT);  // initialize the Buzzer pin as an input
  pinMode(ovenPin, OUTPUT);  // initialize the Magnetron pin as an input
}

void loop()
{
  if (digitalRead(switchPin) == HIGH)
   digitalWrite(ovenPin, HIGH);
   delay (10000);
   digitalWrite(ovenPin, LOW);
   
     for (int counter = 1 ; counter <= 5; counter = counter +1)
      {  digitalWrite(buzzerPin, HIGH);
         delay (200);
         digitalWrite(buzzerPin, LOW);
         delay (100);}
  
              // there is no if statement
  else
    {
    digitalWrite( ledPin, LOW);
    }
}

Use the Auto Format tool in the Arduino IDE:

int switchPin = 2;
int buzzerPin = 7;
int ovenPin = 13;

void setup() 
{ 
  pinMode(switchPin, INPUT);    // initialize the switch pin as an output
  pinMode(buzzerPin, OUTPUT);  // initialize the Buzzer pin as an input
  pinMode(ovenPin, OUTPUT);  // initialize the Magnetron pin as an input
}

void loop() 
{
  if (digitalRead(switchPin) == HIGH)
  {
    digitalWrite(ovenPin, HIGH);
    delay (10000);
    digitalWrite(ovenPin, LOW);
  }
  { 
    for (int counter = 1 ; counter <= 5; counter = counter +1)
    {  
      digitalWrite(buzzerPin, HIGH);
      delay (200);
      digitalWrite(buzzerPin, LOW);
      delay (100);
    }
  }
  else
  {
    digitalWrite(buzzerPin, LOW);
    digitalWrite(ovenPin, LOW);
  }
}
  }
  {

Rarely does this ever look right; you shouldn’t be using curly braces to surround anything unless it is a block of code proceeding if statements, loops and functions.

 if (digitalRead(switchPin) == HIGH)
  {
    ...
  }
  for (int counter = 1 ; counter <= 5; counter = counter +1)
  {  
    ...
  }
  else
  {
    ...
  }

First you have to decide if your for statement should be inside your if block, if so, move it in there, if not, don’t wedge it between an if and else statement.

Your if statement ends here:

  delay (10000);
  digitalWrite(ovenPin, LOW);
   }

Move the closing brace down to just above the else so that it contains the for loop as well. Then the issue with the else will be resolved.

You don't actually need the else clause at all though your outputs will default to low and you leave them so at the end of the process that occurs when your button is pressed.

steinie44:

int switchPin = 2;

int buzzerPin = 7;
int ovenPin = 13;

void setup()
{
  pinMode(switchPin, INPUT);    // initialize the switch pin as an output
  pinMode(buzzerPin, OUTPUT);  // initialize the Buzzer pin as an input
  pinMode(ovenPin, OUTPUT);  // initialize the Magnetron pin as an input
}

void loop()
{
  if (digitalRead(switchPin) == HIGH)
   digitalWrite(ovenPin, HIGH);
   delay (10000);
   digitalWrite(ovenPin, LOW);
   
     for (int counter = 1 ; counter <= 5; counter = counter +1)
      {  digitalWrite(buzzerPin, HIGH);
         delay (200);
         digitalWrite(buzzerPin, LOW);
         delay (100);}
  
              // there is no if statement
  else
    {
    digitalWrite( ledPin, LOW);
    }
}

so it doesn’t recognise my previous if? IM sorry if im sounding stupid, but Ive taught PBasic for years and now tryign to convert to Arduino. in PBasic and if had to be followed by a then command (else in Arduino) is this not the case?

Ive deleted the else commands hoping this would work, but this doesnt work - my buzzer is just constantly pulsing and the LED ive set up to pin 13 does not go on at all. When the switch is pressed, the LED stops instead of the continuing the program like I want

pmacbeath:
so it doesn't recognise my previous if? IM sorry if im sounding stupid, but Ive taught PBasic for years and now tryign to convert to Arduino. in PBasic and if had to be followed by a then command (else in Arduino) is this not the case?

Ive deleted the else commands hoping this would work, but this doesnt work - my buzzer is just constantly pulsing and the LED ive set up to pin 13 does not go on at all. When the switch is pressed, the LED stops instead of the continuing the program like I want

It's not an Arduino language, it's C++. Any C++ references regarding syntax are sufficient.

Hi folks - I got it!!!! thank you very much for your help - its so good to have people like yourselves willing to help a beginner like me. XD

Great stuff..... but do yourself a big favour and embrace Blink Without Delay before you get those pesky delays embedded too deeply in your psyche.

You'll soon need to do something while the code's in the middle of a delay and you'll not be able to.

Correct. Sorry, my bad, I removed the { to trouble shoot it. Need the { after the if statement and } just before the else.
Glad you got it. I was trying to help without doing it so you would learn from it. Good you got it.