I have a processing sketch which is working fine and an Arduino sketch that takes a command for incomingByte and is supposed to run a servo forward or backward accordingly (in response to. When I run it through Processing, SOMETHING is going on but not what I want (servo should run backward on incomingByte=1 and forward on the rest and then pause... Any clues? It's teeth gnashing time here. The Processing sketch is attached. Here's my Arduino sketch:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int myServo = 9; // Control pin for servo motor
int minPulse = 200; // Minimum servo position
int maxPulse = 2400; // Maximum servo position
int pulse = 2400; // Amount to pulse the servo
//
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 100; // the time needed in between pulses
//
int incomingByte = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(myServo, OUTPUT); // Set servo pin as an output pin
Serial.begin(9600);
// Set the motor position value to the middle
pulse = (maxPulse-minPulse)/2;
digitalWrite(myServo, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(myServo, LOW); // Turn the motor off
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
{
for(pulse = 0; pulse < 180; pulse += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pulse); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pulse = 180; pulse >=180; pulse-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
if(incomingByte = 1){ // 1 = value that is comming from processing
if(pulse<maxPulse){
pulse +=1;
}
// }
if(incomingByte = 2){ // 2 = value that is comming from processing
if(pulse>minPulse){
pulse -=1;
}
// }
if(incomingByte = 3){ // 3 = value that is comming from processing
if(pulse>minPulse){
pulse -=1;
}
// }
if(incomingByte = 4){ // 4 = value that is comming from processing
if(pulse>minPulse){
pulse -=1;
delay(1);
}
}
//
// pulse the servo again if the refresh time (15 ms) have passed:
if (millis() - lastPulse > refreshTime) {
digitalWrite(myServo, LOW); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(myServo, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
}
}
}
}
katelake:
I have a processing sketch which is working fine and an Arduino sketch that takes a command for incomingByte and is supposed to run a servo forward or backward accordingly (in response to. When I run it through Processing, SOMETHING is going on but not what I want (servo should run backward on incomingByte=1 and forward on the rest and then pause... Any clues? It's teeth gnashing time here. The Processing sketch is attached. Here's my Arduino sketch:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int myServo = 9; // Control pin for servo motor
int minPulse = 200; // Minimum servo position
int maxPulse = 2400; // Maximum servo position
int pulse = 2400; // Amount to pulse the servo
//
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 100; // the time needed in between pulses
//
int incomingByte = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(myServo, OUTPUT); // Set servo pin as an output pin
Serial.begin(9600);
// Set the motor position value to the middle
pulse = (maxPulse-minPulse)/2;
digitalWrite(myServo, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(myServo, LOW); // Turn the motor off
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
//=================================================
// why do you swing the servo + then - 180 degrees?
// this happens every time you get a byte of data!
//=================================================
{
for(pulse = 0; pulse < 180; pulse += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pulse); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
//========================================
// I think this should be
// for (pulse = 180; pulse >=0; pulse -=1)
//========================================
for(pulse = 180; pulse >=180; pulse-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
//================================================
// end of strange +/- 180 degree swing
//================================================
//================================
// are you expecting 1 or character "1"?
// as noted later you use == to test!
//================================
if(incomingByte == 1){ // 1 = value that is comming from processing
if(pulse<maxPulse){
pulse +=1;
}
}
//=======================================================================
// we already know incomingByte = 1, so it would never be any other value!
// so nothing south of here would ever get to execute
// wonder why you commented out the "}" above (and below)
// I put them back!
//=======================================================================
if(incomingByte == 2){ // 2 = value that is comming from processing
if(pulse>minPulse){
pulse -=1;
}
}
//==============================================
// code for value 2, 4 and 4 seem to be the same
//==============================================
if(incomingByte == 3){ // 3 = value that is comming from processing
if(pulse>minPulse){
pulse -=1;
}
}
if(incomingByte == 4){ // 4 = value that is comming from processing
if(pulse>minPulse){
pulse -=1;
delay(1);
}
}
//
// pulse the servo again if the refresh time (15 ms) have passed:
if (millis() - lastPulse > refreshTime) {
digitalWrite(myServo, LOW); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(myServo, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
}
well first thing I did was put it inside [ code] ... [ /code] !!
then I added some comments
finally I replaced the "}" in your tests at the end
without them only the "incomingByte=1" part would ever get executed
are you sure you mean 1 and not character 1?
depends how you are generating the data
{
for(pulse = 0; pulse < 180; pulse += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pulse); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pulse = 180; pulse >=180; pulse-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
If there is serial data, wave the servo through its full range of motion, slowly. Well, OK. I can't see the purpose of that, but, hey, it appears to be what you want to do.
if(incomingByte = 1){ // 1 = value that is comming from processing
This is assigning the value 1 to incomingByte, not comparing anything. Comparison uses ==.
Hi all. I guess I should say that the incomingBytes are responding to a Processing sketch that is looking at a particular color (RG or B) and whatever the iteration of that color, it either reverses or advances. I wanted it to stop briefly after each move but couldn't figure out how to do that either (yes, I'm newb-squared)