#include <Stepper.h>
#include <WiFi.h>
#include <ThingSpeak.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long myChannelNumber = 2641647;
const char *myWriteAPIkey = "JEVDGKE2UQD9JE9G";
int LBMotorHoriPin = 33, LBMotorHoriValue;
int LBMotorStepPin = 14;
int LBMotorDirectionPin = 27;
int LBMotorSteps = 10, LBMotorDegree = 0, LBMotorMaxDegree = 30;
Stepper LBMotor(LBMotorSteps, LBMotorStepPin, LBMotorDirectionPin);
int UBMotorVertPin = 32, UBMotorVertValue;
int UBMotorStepPin = 13;
int UBMotorDirectionPin = 12; // Fixed pin name
int UBMotorSteps = 5, UBMotorDegree = 0, UBMotorMaxDegree = 10;
Stepper UBMotor(UBMotorSteps, UBMotorStepPin, UBMotorDirectionPin); // Fixed motor pin
int baseMotorHoriPin = 34, baseMotorHoriValue;
int baseMotorStepPin = 2;
int baseMotorDirectionPin = 4; // Fixed pin name
int baseMotorSteps = 15, baseMotorDegree = 0, baseMotorMaxDegree = 90;
Stepper baseMotor(baseMotorSteps, baseMotorStepPin, baseMotorDirectionPin);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
LBMotor.setSpeed(100);
UBMotor.setSpeed(100);
baseMotor.setSpeed(100);
pinMode(LBMotorHoriPin, INPUT);
pinMode(LBMotorStepPin, OUTPUT);
pinMode(LBMotorDirectionPin, OUTPUT);
pinMode(UBMotorVertPin, INPUT);
pinMode(UBMotorStepPin, OUTPUT);
pinMode(UBMotorDirectionPin, OUTPUT);
pinMode(baseMotorHoriPin, INPUT);
pinMode(baseMotorStepPin, OUTPUT);
pinMode(baseMotorDirectionPin, OUTPUT);
}
void loop() {
connectToCloud();
LBMotorHoriValue = map(analogRead(LBMotorHoriPin), 0, 4095, -1, 1); // Fixed variable name and mapping
LBMotorDegree = rotateMotor(LBMotor, LBMotorHoriValue, LBMotorSteps, LBMotorDegree, LBMotorMaxDegree);
Serial.print(LBMotorDegree);
Serial.print(",");
UBMotorVertValue = map(analogRead(UBMotorVertPin), 0, 4095, -1, 1); // Fixed variable name and mapping
UBMotorDegree = rotateMotor(UBMotor, UBMotorVertValue, UBMotorSteps, UBMotorDegree, UBMotorMaxDegree);
Serial.print(UBMotorDegree);
Serial.print(",");
baseMotorHoriValue = map(analogRead(baseMotorHoriPin), 0, 4095, -1, 1); // Fixed mapping
baseMotorDegree = rotateMotor(baseMotor, baseMotorHoriValue, baseMotorSteps, baseMotorDegree, baseMotorMaxDegree); // Fixed assignment
Serial.print(baseMotorDegree);
Serial.println(".");
writeData();
}
int rotateMotor(Stepper motor, int pinValue, int steps, int degree, int maxDegree) {
if (pinValue == 1) {
motor.step(steps);
if (degree < maxDegree)
degree += steps;
} else if (pinValue == -1) {
motor.step(-steps);
if (degree > 0)
degree -= steps; // Fixed typo
}
return degree;
}
void connectToCloud() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
for (int i = 0; i < 5; i++) {
Serial.print(".");
delay(1000);
}
}
Serial.println("\nConnected");
}
}
int statusCode;
void writeData() {
ThingSpeak.setField(1, baseMotorDegree); // Fixed setField spelling
ThingSpeak.setField(2, LBMotorDegree); // Fixed setField spelling
ThingSpeak.setField(3, UBMotorDegree); // Fixed setField spelling
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIkey); // Fixed writeFields spelling and usage
if (statusCode == 200)
Serial.println("Channel update successful.");
else
Serial.println("Problem writing data. HTTP error code:" + String(statusCode));
delay(1000);
}