code pot stepper

This commit is contained in:
boutrie.eytan 2023-06-27 11:14:02 +02:00
parent f99cb0c0bb
commit addd364b61

View file

@ -1,66 +1,49 @@
#include <WiFi.h> const int stepPin = 14;
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
char ssid[] = "cohabit"; // your network SSID (name)
char pass[] = "lewifidecohabit"; // your network password
// A UDP instance to let us send and receive packets over UDP const int dirPin = 12;
WiFiUDP Udp;
const unsigned int localPort = 16403; // local port to listen for UDP packets (here's where we send the packets)
int stepPin = 14;
int dirPin = 12;
void setup() int customDelay,customDelayMapped; // Defines variables
{
Serial.begin(115200);
// initialize LED digital pin as an output.
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) { void setup() {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected"); // Sets the two pins as Outputs
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); Serial.begin(115200);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(localPort);
digitalWrite(dirPin, HIGH);
} }
void step(OSCMessage &msg) { void loop() {
switch (msg.getInt(0)) {
case 0:
digitalWrite(stepPin, LOW);
break; customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
case 1: // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
break; digitalWrite(stepPin, HIGH);
}
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
Serial.println(customDelayMapped);
} }
void loop() // Function for reading the Potentiometer
{
OSCMessage msg; int speedUp() {
int size = Udp.parsePacket();
int customDelay = analogRead(6); // Reads the potentiometer
int newCustom = map(customDelay, 0, 8191, 300,3000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
if (size > 0) {
while (size--) {
msg.fill(Udp.read());
}
//step(msg);
step(msg);
}
} }