Switchable PWM project

Take a search engine of your choice and ask the WWW for '26:1: error: 'Loop' does not name a type; did you mean 'loop'?' and learn how other users provide solutions.

Tinkercad Arduino simulator

really no one can point me in the right direction

As I said, that error message was not caused by the sketch that you posted, or if it did then Tinkercad has bugs in it

Please copy the code that causes the error from Tinkercad into a new reply here

I'v remove one of the void loops and when i press on switch on simulator the simulated servo just wiggle and doesn't go to 40% as per sketch when i press the other simulated switch nothing happens.

#include <Servo.h>

Servo PWM;  // Produces a pwm output based on servo angle

int SwitchLow = 2;  // Pin designation for Switch Medium Low
int SwitchMed = 3;  // Pin designation for Switch Medium Speed
int val;    // variable to read the value from the analog pin

void setup() {
  PWM.attach(9);  // attaches the servo on pin 9 to the servo object
}


void loop() {
  val = digitalRead(SwitchLow);            // reads SwitchLow
  val = map(val, 0, 1, 0, 45);     // Once Switch Low pressed Arduino will output 40% duty cycle of PWM
  PWM.write(val);                  // sets the PWM according to the scaled value
  delay(15); // waits 15ms

  val = digitalRead(SwitchMed);            // reads SwitchMed
  val = map(val, 0, 1, 0, 60);     // Once Switch Med pressed Arduino will output 60% duty cycle of PWM
  PWM.write(val);                  // sets the PWM according to the scaled value
  delay(15);                           // waits 15ms
  }

what does the Val, 0, 1, 0, 45 mean i understand the 45 is the angle output of pwm signal but what doe the other values mean?


image of simple circuit.

Corrected negative to switch on left. now when I press either switches the simulated servo just wiggles a few degrees

That sketch compiles OK

You cannot just add a second function whose name implies that it is a loop and have it work

An Arduino sketch must have a function named setup() and a function named loop(). setup(), as its name implies, runs once and you use it to set things up. loop() repeats forever but only because there is hidden code that calls it again and again in the background

Try this sketch. I cannot guarantee that it does what you want but it does compile

#include <Servo.h>

Servo PWM;  // create servo object to control a servo

int SwitchLow = 2;  // Pin designation for Switch Medium Low
int SwitchMed = 3;  // Pin designation for Switch Medium Speed
int val;            // variable to read the value from the analog pin

void setup()
{
    PWM.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
    val = digitalRead(SwitchLow);  // reads SwitchLow
    val = map(val, 0, 1, 0, 45);   // Once Switch Low pressed Arduino will output 40% duty cycle of PWM
    PWM.write(val);                // sets the PWM according to the scaled value
    delay(15);                     // waits 15ms

    val = digitalRead(SwitchMed);  // reads SwitchMed
    val = map(val, 0, 1, 0, 60);   // Once Switch Med pressed Arduino will output 60% duty cycle of PWM
    PWM.write(val);                // sets the PWM according to the scaled value
    delay(15);                     // waits 15ms
}

If it works then study how it is put together, ie a setup() function and a loop() function

NOTE that there is more that it should contain, but we can get to that, and it could also be made much simpler

yeah i have tried that already, but both switch just produce a wiggling servo. How do i get each of the buttons to give individual function ?

OK. We have made progress. You have a sketch that compiles and does something. Now we can improve it

The problem with it at the moment is that whether or not SwitchLow is pressed the value of what is returned by digitalWrite() is written to the servo and the same for SwitchMed

One solution is only to do something when one or other of the switches is actually pressed and otherwise leave the output as it is

Try this sketch

#include <Servo.h>

Servo PWM;  // create servo object to control a servo

int SwitchLow = 2;  // Pin designation for Switch Medium Low
int SwitchMed = 3;  // Pin designation for Switch Medium Speed
int val;            // variable to read the value from the analog pin

void setup()
{
    PWM.attach(9);                     // attaches the servo on pin 9 to the servo object
    pinMode(SwitchLow, INPUT_PULLUP);  //set the modes for the inputs explicitly
    pinMode(SwitchMed, INPUT_PULLUP);  //they will both be held HIGH until pressed
}

void loop()
{
    val = digitalRead(SwitchLow);  // reads SwitchLow
    if (val == LOW)                //it is pressed
    {
        PWM.write(45);  // output a value of 45
        delay(15);      // waits 15ms
    }

    val = digitalRead(SwitchMed);  // reads SwitchMed
    if (val == LOW)                //it is pressed
    {
        PWM.write(60);  // output a value of 60
        delay(15);      // waits 15ms
    }
}

NOTE wire the switch inputs so that they are taken to GND when the switches are closed

Once again the sketch could be improved but let's try it first

won't compile on simulator

In function 'void loop()':
30:5: error: expected '}' at end of input

very last line!

very silly question why do i have to change the wiring of the switch its the exact way arduino tutorial says?

I would guess you did not copy the final closing } into Tinkercad

I will explain the wiring change when you have it working

needed a } below line 30 so it has compiled however the simulated servo is now wiggling as 45 degrees!

Congratulations, you just debugged and fixed the code

Did you change the wiring ?

i cannot wire two switch outputs to ground !

Why not ?

Oh, and they are inputs, not outputs