I'm working on a project with four potentiometers, two LDRs, and two continuous servomotors. Everything is connected to and powered by the Arduino.
I've begun to notice that when I power the servomotor and tell it to start spinning, the values output by the potentiometers and the LDR start to fluctuate exponentially. For example, I'll have my LDR reading 30 consistently, then it'll randomly spike to 100 for a second; the potentiometers will go from 0 to 1023 at random without being moved at all. This does not happen when the servomotor isn't spinning.
What could possibly be the cause of this? And what could be an alternative to solve this?
The Arduino 5V and GND are going to a breadboard, and the servomotor is connected there.
This is my code for the servomotor and the LDR. My LDR goes into analog pin 4 and the servo signal is connected to digital pin 6; I output the results to the serial port so I can use it in a Processing sketch later.
My bad, I forgot to add that as I was writing over the relevant parts of the code, but it is defined there now! I edited the post to also include an image now.
Also, the atypical way you're printing to the serial monitor: seems like a clunky way to interface with Processing. You can just do Serial.println("whatever"); and Processing will look for "whatever" with no issues if that's what you're trying to do.
Need all the code. Don't worry, I won't sell it to the CIA
I suspect your problem is that you are powering the servo from the Arduino. It should be only signal and ground to Arduino, +5V and ground from a separate power supply and connect the grounds together.