mirror of
https://github.com/Mukheem/TwinTurbine.git
synced 2025-05-09 02:30:35 +02:00
Number format in Ardunio
This commit is contained in:
parent
4f239bed4c
commit
29bd45fba8
TangibleSketches
Arduino/Arduino_Sketch_copy_20240512144111/Arduino_Sketch_copy_14May
ESP32/ESP32_sketch_copy_20240512144341/ESP32_sketch_copy_14May
54
TangibleSketches/Arduino/Arduino_Sketch_copy_20240512144111/Arduino_Sketch_copy_14May/Arduino_Sketch_copy_14May.ino
Normal file
54
TangibleSketches/Arduino/Arduino_Sketch_copy_20240512144111/Arduino_Sketch_copy_14May/Arduino_Sketch_copy_14May.ino
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <Servo.h>
|
||||||
|
|
||||||
|
Servo myservo;
|
||||||
|
int neutralPos = 1520; // Neutral position in microseconds
|
||||||
|
String readString; // String to hold incoming data
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
myservo.attach(7, 400, 2600); // Attach servo to pin 7
|
||||||
|
Serial.begin(9600); // Start serial communication at 9600 baud rate
|
||||||
|
Serial.println("Servo control initialized.");
|
||||||
|
Serial.println("Awaiting commands.");
|
||||||
|
Serial.println("Type an integer to perform a degree rotation.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void performDegreeTurn(int inputMultiplier) { // Add delayMultiplier parameter here
|
||||||
|
int degree = inputMultiplier * 28;
|
||||||
|
int pulseSpeed = 1500; // Speed setting for degree rotation
|
||||||
|
myservo.writeMicroseconds(pulseSpeed);
|
||||||
|
delay(degree); // Time for degree turn
|
||||||
|
myservo.writeMicroseconds(neutralPos); // Stop the servo by resetting to neutral
|
||||||
|
Serial.println("degree turn executed and stopped.");
|
||||||
|
Serial.println("Ready"); // Send confirmation message to ESP32
|
||||||
|
}
|
||||||
|
|
||||||
|
int degree = 0; // Define the degree variable
|
||||||
|
|
||||||
|
void goToZeroDegree() {
|
||||||
|
degree = 10080 - degree; // Subtract the current degree from 20160
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
while (Serial.available()) {
|
||||||
|
char c = Serial.read(); // Read incoming character
|
||||||
|
if (isdigit(c)) { // Check if the character is a digit
|
||||||
|
readString += c; // If so, append it to the readString
|
||||||
|
} else if (c == '\n') { // Check if it's the end of the line
|
||||||
|
// If so, process the received number
|
||||||
|
int number = readString.toInt(); // Convert the string to an integer
|
||||||
|
readString = ""; // Clear the string for new input
|
||||||
|
|
||||||
|
Serial.println("Received: " + String(number)); // Echo what was received
|
||||||
|
|
||||||
|
// Perform actions based on the received number
|
||||||
|
if (0 <= number && number <= 360) {
|
||||||
|
goToZeroDegree();
|
||||||
|
performDegreeTurn(number); // Pass the number as delayMultiplier
|
||||||
|
} else {
|
||||||
|
Serial.println("Invalid command. Please type '1500' to execute a degree turn.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
TangibleSketches/ESP32/ESP32_sketch_copy_20240512144341/ESP32_sketch_copy_14May/ESP32_sketch_copy_14May.ino
Normal file
36
TangibleSketches/ESP32/ESP32_sketch_copy_20240512144341/ESP32_sketch_copy_14May/ESP32_sketch_copy_14May.ino
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
void setup() {
|
||||||
|
Serial.begin(9600); // Start serial communication at 9600 baud rate
|
||||||
|
Serial1.begin(9600); // Start serial communication with Arduino
|
||||||
|
delay(1000); // Allow time for serial monitor to initialize
|
||||||
|
Serial.println("Ready to send commands to Arduino.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (Serial.available()) {
|
||||||
|
String input = Serial.readStringUntil('\n'); // Read input from serial monitor
|
||||||
|
input.trim(); // Remove leading and trailing whitespace
|
||||||
|
|
||||||
|
// Check if the input consists only of numeric characters
|
||||||
|
boolean isNumeric = true;
|
||||||
|
for (size_t i = 0; i < input.length(); i++) {
|
||||||
|
if (!isDigit(input[i])) {
|
||||||
|
isNumeric = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNumeric) {
|
||||||
|
int number = input.toInt(); // Convert the input string to an integer
|
||||||
|
|
||||||
|
// Sending the command to Arduino only if it's within a specific range
|
||||||
|
if (number >= 0 && number <= 1500) {
|
||||||
|
Serial.println("Sending command to Arduino: " + String(number));
|
||||||
|
Serial1.println(number); // Send the command to Arduino via Serial1
|
||||||
|
} else {
|
||||||
|
Serial.println("Invalid command. Please type a number between 0 and 9999.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("Invalid command. Please type a valid number.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user