How to change modes

Hi Alto,

I ran this code:

#include <Servo.h> 

Servo myservo;  // Create servo object
int potPin = A0;  // Analog pin connected to potentiometer

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  myservo.attach(9);  // Attach servo to pin 9
}

void loop() {
  int potValue = analogRead(potPin);  // Read potentiometer value
  int servoVal = map(potValue, 0, 1023, 0, 180);  // Map to servo angle

  myservo.write(servoVal);  // Set servo position
  Serial.print("Potentiometer value: "); 
  Serial.println(potValue);  // Print potentiometer value to serial monitor
  delay(0);  // Small delay for servo movement
} 

And I have the numbers that were printed in serial printer as I rotated the POT and the modes changed of my LED. How do we decipher them?

Potentiometer value: 155
Potentiometer value: 147
Potentiometer value: 147
Potentiometer value: 147
Potentiometer value: 9
Potentiometer value: 7
Potentiometer value: 148
Potentiometer value: 147
Potentiometer value: 9
Potentiometer value: 9
Potentiometer value: 147
Potentiometer value: 148
Potentiometer value: 8
Potentiometer value: 9
Potentiometer value: 148
Potentiometer value: 148
Potentiometer value: 7
Potentiometer value: 8
Potentiometer value: 147
Potentiometer value: 147
Potentiometer value: 9
Potentiometer value: 147
Potentiometer value: 148
Potentiometer value: 11
Potentiometer value: 9
Potentiometer value: 148
Potentiometer value: 147
Potentiometer value: 9
Potentiometer value: 8
Potentiometer value: 147
Potentiometer value: 147
Potentiometer value: 9
Potentiometer value: 9
Potentiometer value: 147
Potentiometer value: 147
Potentiometer value: 9
Potentiometer value: 147
Potentiometer value: 147

What means this? There is no output to drive a LED in that code.

With delay(0) and baud 9600, that code becomes serial-bound very quickly. But, you're only reading 2 values, so I'd say your pot isn't corrected at all, or very loosely.

The code looks good. <checks again> Yes.

But the output doesn't look right at all. Even accounting for

  Serial.println(potValue);  // Print potentiometer value to serial monitor

printing the potValue instead of the mapped servoCal

You should be seeing 0 .. 1023, and when you add or change to printing the servo position value, you should see 0 .. 180.

The value shoukd reflect the twist of the lot and go smoothly between the extremes.

So… are you sure you've wired the pot as the diagram shows? On end to ground, one end to 5 volts, and the middle, or "wiper" to you analog input?

Is your attached servo-controlled LED changing modes with this experimental setup?

Maybe throw us a picture or two of what you have in front of you; just now I can only think it isn't wired correctly.

When you get the servo values to go from 0 to 180 smoothly, just bump it along and note the values where the LED thing changes mode. If it changed modes at 100 and 120, for example, hitting it with 110 later woukd be smack in the middle of one of the modes, and would be a value to carry into the sketch that will ultimately neatly switch modes.

a7

Oh hi. my "servo" in the code is an LED that has it's own chip onboard that requires a signal to change modes. Sorry you didn't gather that from beginning of thread to current.

1 Like

Good point.

@misterbee1 set the band rate to something 21st century-ish, like 115200, serial monitor also to match.

Increase the delay from 0 to 100. It will still print and change plenty fast, 10x per second.

a7

1 Like

Ok, I will do this first before you previous request(s)....

Please help, how do I select ALL of the Serial Printout to paste here? It is only copying a section....

You can hold down the reset button on the boars to stop the output, then drag select in the window.

But do you need to? It should be obvious when it is working... twist slowy to the right the numbers go up, slowly to the left they go down or vice versa and your LED thing should change modes accordianly.

Are the modes changing? Do the numbers look plausible ATM?

a7

Yes looks to be about 680 when the modes changes

See this thread on the mode change
https://forum.arduino.cc/t/using-the-nano-pwm-output-to-controle-rc-rotating-light/1229513/3

This was the solution

Using this, to trigger the mode selector:

myservo.writeMicroseconds(2000);
delay(20);
myservo.writeMicroseconds(1000);

Thank you, but I am waiting for my friend alto to confirm this..... alto, let me know what I have to do.. I have all kinds of switches avail...... my ultimate result is to have a push button switch to be able to switch between these values.

Instead of trying to infer what your code is doing from LED behavior, take a step back and print what you're writing to Servo. It may be that the LED is doing exactly what it should, with whatever Servo is receiving.

Sry, I just happened to see this. You can @alto777 me (or anyone) to ensure a notification.

I think it is worth trying the pulse @cattledog recommends. It worked in the other thread and is entirely plausible.

So… rather than ranges of "servo" deflection corresponding to modes, the pulse would advance through the modes, one pulse, mode 2, one pulse , mode 3 and then it would wrap around and a pulse woukd return to mode 1.

It's less controllable, as there is no way for the Arduino board to know what the mode is, but you will achieve your goal of having a pushbutton change the mode.

To try it, arrange code to make the pulse (print something to say you did) and then a delay of 15000 (fifteen seconds) so you have time to see the new (we hope) mode kick in.

Once that works, you can move on to making the pulse as a response to pressing a button.

So when that bridge comes, you may need to learn about how to wrangle a button press. I f you don't already know.

You might even be able to do it with the code you have now. Just substitute a push button you digital read to put out either 1000 or 2000 to the servo depending it is pressed or not. It may not need to be any certain length, but stab the button if you try it. Stab stab stabbity stab.

BTW writing to the "servo" with the function you use now, with 0 and 180 degrees as they are equivalent to microseconds 1000 to 2000.

a7

Since the OP in the thread @cattledog linked was able to do the mode advancing using an on/off switch, the very brief pulse afforded by

  myservo.writeMicroseconds(2000);
  delay(20);
  myservo.writeMicroseconds(1000);

or if we use degrees as can be done

  myservo.write(180);
  delay(20);
  myservo.write(0);

can be very longer. In fact since servo PWM pulses come at 20 millisecond interval, it seems that using only 20 is cutting it close and I wouldn't be surprised if it didn't work too well. The servo output would be just one pulse that was different and it is not hard for me to imagine that it might never appear if the timing of the calls was just right.

It's hard to press and release a button that fast. I would try 60 or 100 milliseconds to ensure that the long pulse appears.

# include <Servo.h> 

int buttonPin = A0;
int servoPin = 9;

Servo myServo;

void setup() {
  Serial.begin(9600);

  pinMode(buttonPin, INPUT_PULLUP);   // wire button between pin and ground; no resistor

  myServo.attach(servoPin);
  myServo.write(0);         // r/c switch OFF
}

bool lastPressed;  // was the button down last time we looked?

void loop() {
  
  bool isPressed = digitalRead(buttonPin) == LOW;

// if the button is different to last time
  if (lastPressed != isPressed) {
// and is now being pressed
    if (isPressed) {
      Serial.println("I see that button!");
// it must have just went down, so do the servo thing
      myServo.write(180);   // r/c switch ON
      delay(100);
      myServo.write(0);     // r/c switch OFF
    }
    lastPressed = isPressed;
  }

  delay(25);    // poor man's debounce
}

I can't test this, but it should be close.

HTH

a7

1 Like

Alto,

I have uploaded your code and added button ( I think correctly ).

Here is pic of current build:


Serial Printer says "I see that button!!"

When I press it though, mode does not change. LED turns off briefly as if USB was disconnected and reconnected really quick. What are you thoughts?

Fixed button wiring:

Now TX blinks really quick once with one push of button, but no mode change for LED.

1 Like

i think not!

Use only two pins on the pushbutton, either pair of diagonally opposite pins.

You are now shortening out and resetting the Arduino power supply which is a Bad Idea.

a7

Fixed it. See #41.

Back when servo sweep worked, did the mode change once per sweep?

If you couldn't tell, add printing to that servo sweep code what worked and see if the mode changes once per sweep.

Make a longer pause between sweeps if it is still hard to tell when the mode changes.

I hope it changes once per sweep. If so doing it the way it is in the latest button-operated sketch should… also change the mode.

I got nothing if it is still not working.

It is less than desirable to power the LED thing for the Arduino 5+ pin, but it worked OK back when, so for now I don't suspect it as the root of the trouble ATM.

I'll try to think of another experiment. Just how, I think between the pot twiddling, which I can't remember if you ever got working smoothly, and the pushbutton, something should work.

Your wiring looks OK. But examine it closely again, make sure the wires are in the breadboard full and accurate &c.

Confirm one "saw that button" per button presss.

And what the what - to be thorough and it's easy enough, try changing the delay to 50 and then 20, but I dont think that should matter.

a7