diff --git a/arduino_code/motor_control.ino b/arduino_code/motor_control.ino index 74102641df5bd6b280114e13161c7331772e72c9..bdbb6beeb362a004071cc90ff6bb10e27e26c33f 100644 --- a/arduino_code/motor_control.ino +++ b/arduino_code/motor_control.ino @@ -1,25 +1,33 @@ + +const int motorPin1 = 27; +const int motorPin2 = 26; +const int motorPin3 = 15; +const int motorPin4 = 13; + +AccelStepper myStepper(AccelStepper::FULL4WIRE, motorPin1, motorPin2, motorPin3, motorPin4); + void setup() { // put your setup code here, to run once: - serial.begin(9600) + Serial.begin(9600) float steps_per_rotation=200; float start_max_speed = 200; // in rpm, arbitrary start max speed, for testing***** - setMaxSpeed(start_max_speed); // based on chart maybe 1000 but that seems way too fast - setSpeed(start_max_speed*steps_per_rotation*60); // ***testing, may work for constant speed + myStepper.setMaxSpeed(start_max_speed); // based on chart maybe 1000 but that seems way too fast + myStepper.setSpeed(start_max_speed*steps_per_rotation*60); // ***testing, may work for constant speed float min_speed = 20; // set to be the speed at which we apply the goal torque, based on chart maybe 600 but that seems too fast float same_step_range = 10; // min number of steps allowed before increasing the same position count, test this range***** float same_position_count = 0; // number of consecutive steps where the current position is within same_step_range as the previous position - float previous_pos=currentPosition()-same_step_range + float previous_pos=myStepper.currentPosition()-same_step_range float new_pos=200 // 200(1 rotation) is arbitrary number of steps to increase each iteration, **possibly make new target proportional to speed float reduce_rpm = 12; // when using set speed, reduces speed by 40 steps per second. => 40*60/200 = 2400/200 = 12rpm (rpm/0.3=num) } void loop() { // put your main code here, to run repeatedly: - runToPosition(1) + myStepper.runToPosition(1) while(cur_speed>=min_speed){ - runToNewPosition(targetPosition()+new_pos); - if(currentPosition()-previous_pos<same_step_range){ + myStepper.runToNewPosition(myStepper.targetPosition()+new_pos); + if(myStepper.currentPosition()-previous_pos<same_step_range){ same_position_count+=1; } else{ @@ -27,9 +35,9 @@ void loop() { } if(same_position_count>1){ // check if it has stayed in the same position for the past two turns. // setMaxSpeed(cur_speed-=40); // use if there is no way to implement this with the constant speed function. - setSpeed(speed()-(reduce_rpm/0.3)); + myStepper.setSpeed(myStepper.speed()-(reduce_rpm/0.3)); } - previous_pos=currentPosition() + previous_pos=myStepper.currentPosition() } diff --git a/arduino_code/test_wificircuit/index.h b/arduino_code/test_wificircuit/index.h new file mode 100644 index 0000000000000000000000000000000000000000..462ded7ad1933185fbf904f292153b2b987c550f --- /dev/null +++ b/arduino_code/test_wificircuit/index.h @@ -0,0 +1,44 @@ +const char MAIN_page[] PROGMEM = R"=====( +<!DOCTYPE html> +<html> +<style> +.card{ + max-width: 400px; + min-height: 250px; + background: #02b875; + padding: 30px; + box-sizing: border-box; + color: #FFF; + margin:20px; + box-shadow: 0px 2px 18px -4px rgba(0,0,0,0.75); +} +</style> +<body> + +<div class="card"> + <h4>The ESP32 Update web page without refresh</h4><br> + <h1>Sensor Value:<span id="ADCValue">0</span></h1><br> + <br><a href="https://circuits4you.com">Circuits4you.com</a> +</div> +<script> + +setInterval(function() { + // Call a function repetatively with 2 Second interval + getData(); +}, 1000); //2000mSeconds update rate + +function getData() { + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + document.getElementById("ADCValue").innerHTML = + this.responseText; + } + }; + xhttp.open("GET", "readADC", true); + xhttp.send(); +} +</script> +</body> +</html> +)====="; \ No newline at end of file diff --git a/arduino_code/test_wificircuit/test_wificircuit.ino b/arduino_code/test_wificircuit/test_wificircuit.ino new file mode 100644 index 0000000000000000000000000000000000000000..05e299903c2dcaeb4b073a06891456ff95bc1209 --- /dev/null +++ b/arduino_code/test_wificircuit/test_wificircuit.ino @@ -0,0 +1,123 @@ +/* + * ESP32 AJAX Demo + * Updates and Gets data from webpage without page refresh + * https://circuits4you.com + */ +#include <WiFi.h> +#include <WiFiClient.h> +#include <WebServer.h> + +#include "index.h" //Web page header file +#include <arduino-timer.h> + +WebServer server(80); + +//Enter your SSID and PASSWORD +const char* ssid = "Connectify-ESP32 Boot"; +const char* password = "temp_pass1234"; +int timerCounter = 0; + +int intervals[13]; +int intervalValues[13]; + +//=============================================================== +// This routine is executed when you open its IP in browser +//=============================================================== +void handleRoot() { + String s = MAIN_page; //Read HTML contents + server.send(200, "text/html", s); //Send web page +} + +bool handlePressureIntervalCall (void *argument) { + int a = analogRead(34); + int aModulus = a % 300; + intervals[aModulus] += 1; + intervalValues[aModulus] += a; + + timerCounter ++; + Serial.println("Value read: " + a ); + Serial.println("Timer counter: " + timerCounter); + + if (timerCounter == 10) { + return false; + timerCounter = 0; + } + else { + return true; + } +} + +void handleADC() { + intervals[13]; + intervalValues[13]; + + Timer<> timer; + + timer.every(10, handlePressureIntervalCall); + timer.tick(); + + int maxIntervalSize = intervals[0]; + int maxIntervalIndex = 0; + for (int index = 1; index < 13; index ++) { + if (intervals[index] > maxIntervalSize) { + maxIntervalIndex = index; + maxIntervalSize = intervals[index]; + } + } + Serial.println("maxIntervalIndex: " + maxIntervalIndex); + int adcValueInt = intervalValues[maxIntervalIndex]/maxIntervalSize; + String adcValue = String(adcValueInt); + Serial.println("adcValue: " + adcValue); + Serial.println("******************************"); + + server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request +} + +//=============================================================== +// Setup +//=============================================================== + +void setup(void){ + Serial.begin(115200); + Serial.println(); + Serial.println("Booting Sketch..."); + +/* +//ESP32 As access point + WiFi.mode(WIFI_AP); //Access Point mode + WiFi.softAP(ssid, password); +*/ +//ESP32 connects to your wifi ----------------------------------- + WiFi.mode(WIFI_STA); //Connectto your wifi + WiFi.begin(ssid, password); + + Serial.println("Connecting to "); + Serial.print(ssid); + + //Wait for WiFi to connect + while(WiFi.waitForConnectResult() != WL_CONNECTED){ + Serial.print("."); + } + + //If connection successful show IP address in serial monitor + Serial.println(""); + Serial.print("Connected to "); + Serial.println(ssid); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); //IP address assigned to your ESP +//---------------------------------------------------------------- + + server.on("/", handleRoot); //This is display page + server.on("/readADC", handleADC);//To get update of ADC Value only + + server.begin(); //Start server + Serial.println("HTTP server started"); +} + +//=============================================================== +// This routine is executed when you open its IP in browser +//=============================================================== +void loop(void){ + server.handleClient(); + delay(1); +} \ No newline at end of file