Forum

Stream arduino step outputs into one value

Hi

I have my vvvv set up so that it counts each time a button is pressed on my arduino. This button also makes something move on the arduino board a certain amount of steps. So I want the steps appearing on my vvvv screen to be the same as what’s happening in real life. However, the values keep coming in at a range, e.g. 6,7,8,9 when it’s only moving 6 steps on the board. The board is set to always move 6 steps. I thought an if loop of some sort might work, i.e. if 6,7,8,8,9 equals 6, or else a series of or gates, but I can’t seem to find a vvvv equivalent. Has anyone dealt with anything similar?

Thanks,
Rochelle

hi, can u post your arduino code and/or your (simplified) v4 patch to troubleshoot your issue? personally i would only let one program handle the counting, so you could simply send your counter value also to v4 or let v4 do the counting and send it back to the arduino.

anyhow, with your code we can give you a proper solution.

best, leo

Hi Leo,

This is the arduino code. The output is at the end of each movement loop.

// Define
#define dirPin 2
#define stepPin 3
#define forward 10
#define backward 9
#define stepsPerRevolution 7
#define stopbackward 4
#define stopforward 5
#define clean 7
#define pause 2000
#define pause2 250
#define pause3 175
#define cleanstep 900
#define positionf 8
#define positionb 12

int forwards = 0;
int backwards = 0;
int stopforwards = 0;
int stopbackwards = 0;
int steps;
int cleans = 0;
int centredist = 0;
int steps2;
int steps3;
int fullrot = 420;

void setup() {
Serial.begin(9600);
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(positionf, OUTPUT);
pinMode(positionb, OUTPUT);

pinMode(forward, INPUT);
pinMode(backward, INPUT);
pinMode(stopforward, INPUT);
pinMode(stopbackward, INPUT);
pinMode(clean, INPUT);

while (!digitalRead(stopforward)){
digitalWrite(dirPin,HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(pause);
digitalWrite(stepPin,LOW);
delayMicroseconds(pause);
}
for (int i = 0; i < 400; i++){
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(pause);
digitalWrite(stepPin,LOW);
delayMicroseconds(pause);
}
steps=0;
steps3=0;
}

void loop() {
forwards = digitalRead(forward);
backwards = digitalRead(backward);
stopforwards = digitalRead(stopforward);
stopbackwards = digitalRead(stopbackward);
cleans = digitalRead(clean);

steps2=(steps*30)/35;

// Serial.write(steps2);

Serial.print(steps2);
// Serial.print(’ ‘);
// Serial.print(steps);
// Serial.print(’ ‘);
// Serial.print(steps3);
Serial.println(’ ');

if(forwards==HIGH){
digitalWrite(dirPin, HIGH);

for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
if (stopforwards==HIGH){
digitalWrite(stepPin, LOW);
break;
}
digitalWrite(stepPin, HIGH);
delayMicroseconds(pause);
digitalWrite(stepPin, LOW);
delayMicroseconds(pause);
steps=steps+1;
steps3=steps3+1;

}
digitalWrite(positionf,HIGH);
delay(pause3);
digitalWrite(positionf,LOW);
delay(pause2);
// Serial.print(steps);
// Serial.println(’ ');

}

if(backwards==HIGH){
digitalWrite(dirPin, LOW);

for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
if (stopbackwards==HIGH){
digitalWrite(stepPin, LOW);
break;
}
digitalWrite(stepPin, HIGH);
delayMicroseconds(pause);
digitalWrite(stepPin, LOW);
delayMicroseconds(pause);
steps=steps-1;
steps3=steps3-1;

}
digitalWrite(positionb,HIGH);
delay(pause3);
digitalWrite(positionb,LOW);
delay(pause2);
// Serial.print(steps);
// Serial.println(’ ');

}

if (cleans==HIGH){
if (steps3 > 0){
digitalWrite(dirPin, LOW);
for (int i = 0; i < fullrot; i++){
if (stopbackwards==HIGH){
digitalWrite(stepPin, LOW);
break;
}
digitalWrite(stepPin, HIGH);
delayMicroseconds(pause);
digitalWrite(stepPin, LOW);
delayMicroseconds(pause);
steps3=steps3-1;
}
}

else{

digitalWrite(dirPin, HIGH);
for (int i = 0; i < fullrot; i++){
if (stopforwards==HIGH){
digitalWrite(stepPin, LOW);
break;
}
digitalWrite(stepPin, HIGH);
delayMicroseconds(pause);
digitalWrite(stepPin, LOW);
delayMicroseconds(pause);
steps3=steps3+1;
}
}
}

}

Thanks,
Rochelle

i’ll modified your arduino code a bit - for the end switches have a look at the arduino documentation about “attach digital interrupt” or in general interrupt pins. Please check the code before running it, especially since i modified the part about your end switches -

as of your problem as i understand it, you would simply like to do a whole integer division by 6.

so one of
{0,1,2,3,4,5} should equal 0
{6,7,8,9,10,11} should equal 1

simply divide your value by 6 and use the frac node to only get the whole part.

if i missunderstood you, pls explain further :D
sketch_jun01a.zip (1.5 KB)

1 Like