Posted on Leave a comment

arduino code:Acrylic Arduino 4DOF robot arm SNM-1100

<pre lang="”LANGUAGE”">#include &lt;Servo.h&gt;

Servo myservo1;

Servo myservo2;

Servo myservo3;

Servo myservo4;

int potpin1 = 0;

int potpin2 = 1;

int potpin3 = 2;

int potpin4 = 3;

int val1;

int val2;

int val3;

int val4;



int Value1;

int Value2;

int Value3;

int Value4;

void setup()

{

 myservo1.attach(11);

 myservo2.attach(10);

 myservo3.attach(9);

 myservo4.attach(6);

 Serial.begin(9600);

}

///////////////////////////////////////////////////

#define FILTER_N 0

int i = 0;

int Filter1() {

 int new_value;

 new_value = analogRead(potpin1);

 if (Value1 != new_value) {

 i++;

 if (i &gt; FILTER_N) {

 i = 0;

 Value1 = new_value;

 }

 }

 else

 i = 0;

 return Value1;

}

/////////////////////////////////////////////////



int j = 0;

int Filter2() {

 int new_value;

 new_value = analogRead(potpin2);

 if (Value2 != new_value) {

 j++;

 if (j &gt; FILTER_N) {

 j = 0;

 Value2 = new_value;

 }

 }

 else

 j = 0;

 return Value2;

}

////////////////////////////////////////////////////

int k = 0;

int Filter3() {

 int new_value;

 new_value = analogRead(potpin3);

 if (Value3 != new_value) {

 k++;

 if (k &gt; FILTER_N) {

 k = 0;

 Value3 = new_value;

 }

 }

 else

 k = 0;

 return Value3;

}

///////////////////////////////////////////////////////

int m = 0;

int Filter4() {

 int new_value;

 new_value = analogRead(potpin4);

 if (Value4 != new_value) {

 m++;

 if (m &gt; FILTER_N) {

 m = 0;

 Value4 = new_value;

 }

 }

 else

 m = 0;

 return Value4;

}

void loop()

{

 val1 = Filter1();

 val1 = map (val1, 0, 1023, 120, 150);

 myservo1.write(val1);

 delay(1);

 Serial.print("val1=");

 Serial.print(val1);

 Serial.print(",");



 val2 = Filter2();

 val2 = map (val2, 0, 1023, 0, 179);

 myservo2.write(val2);

 delay(1);

 Serial.print("val2=");

 Serial.print(val2);

 Serial.print(",");

 val3 = Filter3();

 val3 = map (val3, 0, 1023, 0, 179);

 myservo3.write(val3);

 delay(1);

 Serial.print("val3=");

 Serial.print(val3);

 Serial.print(",");
 

 val4 = Filter4();

 val4 = map (val4, 0, 1023, 40, 179);

 myservo4.write(val4);

 delay(1);

 Serial.print("val4=");

 Serial.print(val4);

 Serial.println(",");



}</pre>
Leave a Reply

Your email address will not be published. Required fields are marked *