Projet

Général

Profil

Objet pneumatique » Valve_servo.ino

Code Arduino pour les servo - Ida Fermaud, 21/12/2021 18:14

 
#include <Servo.h>

Servo servoIn;
Servo servoOut;
int pinServoIn = 9;
int pinServoOut = 11;
int stepDelayTotal = 2000;
String message;

int pos = 0;
int s = 50;// variable to store the servo position
int n;

void setup() {
Serial.begin(9600);
servoIn.attach(pinServoIn);
servoOut.attach(pinServoOut);
servoIn.write(pos);
servoOut.write(pos);
}

void loop() {
if (Serial.available())
{
message = Serial.readString();//String
Serial.println(message);
if (message)
{
n = message.toInt();
s = round(stepDelayTotal / n);
}
}

if (n != 0){
servoOut.write(0);
for (pos = 0; pos <= n; pos += 1) { // in steps of 1 degree
servoIn.write(pos);
delay(s);
}
servoOut.write(90);
for (pos = n; pos >= 0; pos -= 1) {
servoIn.write(pos);
delay(s);
}
}
delay(20);
}
(26-26/30)