code pot stepper
This commit is contained in:
parent
f99cb0c0bb
commit
addd364b61
|
@ -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 customDelay,customDelayMapped; // Defines variables
|
||||||
int stepPin = 14;
|
|
||||||
int dirPin = 12;
|
void setup() {
|
||||||
|
|
||||||
|
// Sets the two pins as Outputs
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
// initialize LED digital pin as an output.
|
|
||||||
pinMode(stepPin,OUTPUT);
|
pinMode(stepPin,OUTPUT);
|
||||||
|
|
||||||
pinMode(dirPin,OUTPUT);
|
pinMode(dirPin,OUTPUT);
|
||||||
Serial.println();
|
|
||||||
Serial.print("Connecting to ");
|
|
||||||
Serial.println(ssid);
|
|
||||||
WiFi.begin(ssid, pass);
|
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
Serial.println("");
|
|
||||||
|
|
||||||
Serial.println("WiFi connected");
|
|
||||||
Serial.println("IP address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
|
|
||||||
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);
|
digitalWrite(stepPin, HIGH);
|
||||||
break;
|
|
||||||
}
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue