#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?
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.
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.
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.
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?
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.
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
}
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.