Hi I have some processing code that basically moves a slider and sends the info to the ardunio so it outputs that to a servo. The arudino code test's the processing code BEFORE sending it out to the servo to ensure that its free from errors.
I know I don't really need to do this and there are libraries for this like firmatta, but I don't want to use those.
The issue is when I upload my arduino code to the board, then close the arduino IDE and start up the processing IDE and run that code, when I move the slider on the processing window, it does not move my servo? I am using a velros sg90 servo and I connect it to pin 3 on my arduino UNO.
Arduino code:
int servo1Pin = 3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(servo1Pin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
}
void serialEvent()
{
//This function is called whenever a serial message (from Processing) is received.
if (Serial.available() > 0)
{
String message = Serial.readString();
String command = getValue(message, '_', 0);
int value = getValue(message, '_', 1).toInt();
if (command.equals("Servo1"))
{
setServo(servo1Pin, value);
}
else if (command.equals("anotherCommand"))
{
//do something
}
else
{
//It's not any programmed message! Must be a mistake
Serial.println("ERROR: invalid message");
}
}
}
String getValue(String data, char separator, int index)
{
//Split a String with a separator character and retrieve the substring at location index
// getValue("test:message", ':', 0) would retrieve "test", getValue("test:message", ':', 1) would retrieve "message"
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void setServo(int pin, int value)
{
//write a value to a pin
if (value > 0 && value < 256)
{
analogWrite(pin, value);
//Assuming it takes 25 ms for the servo to reach its destination (this might not be necessary for every application)
delay(25);
Serial.println("SUCCESS: servo moved");
}
else
{
Serial.println("ERROR: invalid servo position");
}
}
Processing Code:
import processing.serial.*; //imports serail library
import controlP5.*; //Part of processing library
Slider Servo1;
Serial serial;
ControlP5 cp5;
void setup()
{
size(950, 900);
println(Serial.list());
String port = Serial.list()[0];
serial = new Serial(this, port, 9600); //sets COMM Port Baud Rate
cp5 = new ControlP5(this); //dynamic variable of control class
Servo1 = cp5.addSlider(".").setRange(0,180).setValue(90).setPosition(110,134).setSize(270,25).setNumberOfTickMarks(270).setLabelVisible(true)
.setColorForeground(color(102,102,102)).setColorBackground(color(255,153,0)).setColorActive(color(102,102,102));
}
void draw()
{
//will fill alter
background(color(51,102,153)); //background of GUI color in RGB format
println("Servo1" + int(Servo1.getValue()));
}