//
#include <Servo.h>
// setup servo
int servoPin = D4;
int PEN_DOWN = 90; // angle of servo when pen is down
int PEN_UP = 30; // angle of servo when pen is up
Servo penServo;
float wheel_dia=65.9; // # mm (increase = spiral out)
float wheel_base=115; // # mm (increase = spiral in, ccw)
int steps_rev=512; // # 512 for 64x gearbox, 128 for 16x gearbox
int delay_time=6; // # time between steps in ms
// Stepper sequence org->pink->blue->yel
int L_stepper_pins[] = {D7, D5, D6, D8};
int R_stepper_pins[] = {D3, D1, D0, D2};
int fwd_mask[][4] = {{1, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 1},
{1, 0, 0, 1}};
int rev_mask[][4] = {{1, 0, 0, 1},
{0, 1, 0, 1},
{0, 1, 1, 0},
{1, 0, 1, 0}};
void setup() {
randomSeed(analogRead(1));
Serial.begin(9600);
for(int pin=0; pin<4; pin++){
pinMode(L_stepper_pins[pin], OUTPUT);
digitalWrite(L_stepper_pins[pin], LOW);
pinMode(R_stepper_pins[pin], OUTPUT);
digitalWrite(R_stepper_pins[pin], LOW);
}
penServo.attach(servoPin);
Serial.println("setup");
penup();
delay(1000);
}
void loop(){
Serial.println("Enter your name or sequence of letters:");
while (!Serial.available()) {
}
// Read user input from Serial Monitor
String userInput = Serial.readStringUntil('\n');
pendown();
Serial.print("Drawing in progress...");
drawName(userInput);
Serial.print("Drawing completed!!!");
// Optional delay between drawings
delay(1000);
}
// ----- HELPER FUNCTIONS -----------
int step(float distance){
int steps = distance * steps_rev / (wheel_dia * 3.1412); //24.61
Serial.println("Drawing in progress");
return steps;
}
float getNearestAngle(float angle_){
float angle = 0;
int step = 0;
float previousAngle = 0;
float step_length = 3.1412 * wheel_dia / steps_rev;
while(!(previousAngle <= angle_ && angle_ <= angle)){
step += 1;
previousAngle = angle;
angle = step * step_length * 360 / (wheel_base * 3.1412) + 0.01;
}
float dif1 = angle_- angle;
float dif2 = angle_- previousAngle;
if(abs(dif1) < abs(dif2)){
return angle;
}else{
return previousAngle;
}
}
void forward(float distance){
int steps = step(distance);
for(int step=0; step<steps; step++){
for(int mask=0; mask<4; mask++){
for(int pin=0; pin<4; pin++){
digitalWrite(L_stepper_pins[pin], rev_mask[mask][pin]);
digitalWrite(R_stepper_pins[pin], fwd_mask[mask][pin]);
}
delay(delay_time);
}
}
}
void backward(float distance){
int steps = step(distance);
for(int step=0; step<steps; step++){
for(int mask=0; mask<4; mask++){
for(int pin=0; pin<4; pin++){
digitalWrite(L_stepper_pins[pin], fwd_mask[mask][pin]);
digitalWrite(R_stepper_pins[pin], rev_mask[mask][pin]);
}
delay(delay_time);
}
}
}
void right(float degrees){
float rotation = getNearestAngle(degrees) / 360.0;
float distance = wheel_base * 3.1412 * rotation;
int steps = step(distance);
for(int step=0; step<steps; step++){
for(int mask=0; mask<4; mask++){
for(int pin=0; pin<4; pin++){
digitalWrite(R_stepper_pins[pin], rev_mask[mask][pin]);
digitalWrite(L_stepper_pins[pin], rev_mask[mask][pin]);
}
delay(delay_time);
}
}
}
void left(float degrees){
float rotation = getNearestAngle(degrees) / 360.0;
float distance = wheel_base * 3.1412 * rotation;
int steps = step(distance);
for(int step=0; step<steps; step++){
for(int mask=0; mask<4; mask++){
for(int pin=0; pin<4; pin++){
digitalWrite(R_stepper_pins[pin], fwd_mask[mask][pin]);
digitalWrite(L_stepper_pins[pin], fwd_mask[mask][pin]);
}
delay(delay_time);
}
}
}
void done(){ // unlock stepper to save battery
for(int mask=0; mask<4; mask++){
for(int pin=0; pin<4; pin++){
digitalWrite(R_stepper_pins[pin], LOW);
digitalWrite(L_stepper_pins[pin], LOW);
}
delay(delay_time);
}
}
void penup(){
delay(250);
Serial.println("PEN_UP()");
penServo.write(PEN_UP);
delay(250);
}
void pendown(){
delay(250);
Serial.println("PEN_DOWN()");
penServo.write(PEN_DOWN);
delay(250);
}
void A(){
left(70);
forward(110);
left(40);
backward(110);
forward(50);
left(67);
forward(42);
penup();
left(150);
forward(93);
left(33);
pendown();
}
void B(){
left(90);
forward(110);
right(70);
int a = 1;
int b = 1;
while (a<=140) {
forward(1);
right(1.82);
a++;
}
left(45);
while (b<=150) {
backward(1);
right(1.7);
b++;
}
penup();
left(35);
forward(60);
pendown();
}
void c(){
penup();
forward(70);
right(5);
pendown();
int a = 1;
while (a<=230) {
backward(1.2);
right(1);
a++;
}
penup();
right(50);
backward(120);
right(125);
pendown();
}
void d(){
left(90);
forward(110);
right(70);
int a = 1;
while (a<=270) {
forward(1.2);
right(1);
a++;
}
penup();
right(170);
forward(105);
pendown();
}
void e(){
forward(50);
penup();
backward(50);
pendown();
left(90);
forward(55);
right(90);
forward(50);
penup();
backward(50);
left(90);
pendown();
forward(55);
right(90);
forward(50);
penup();
right(75);
forward(114);
left(75);
pendown();
}
void f(){
left(90);
forward(55);
right(90);
forward(50);
penup();
backward(50);
left(90);
pendown();
forward(55);
right(90);
forward(50);
penup();
right(75);
forward(114);
left(75);
pendown();
}
void g(){
penup();
forward(70);
pendown();
left(90);
forward(45);
left(90);
forward(30);
penup();
backward(30);
right(90);
backward(45);
pendown();
right(65);
int a = 1;
while (a<=275){
backward(1.2);
right(1);
a++;
}
penup();
right(50);
backward(120);
right(115);
pendown();
}
void h(){
left(90);
forward(110);
penup();
backward(55);
right(90);
pendown();
forward(50);
penup();
left(90);
forward(55);
pendown();
backward(110);
penup();
right(90);
forward(30);
pendown();
}
void I(){
left(90);
forward(110);
penup();
right(165);
forward(115);
left(75);
pendown();
}
void j(){
penup();
left(90);
forward(30);
pendown();
int a = 1;
while (a <= 90) {
left(2);
backward(1);
a++;
}
backward(80);
penup();
left(10);
forward(108);
left(80);
pendown();
}
void k(){
left(90);
forward(110);
penup();
backward(55);
right(45);
pendown();
forward(75);
penup();
backward(75);
right(90);
pendown();
forward(75);
penup();
left(45);
forward(30);
pendown();
}
void l(){
penup();
left(90);
forward(110);
pendown();
backward(110);
penup();
right(90);
pendown();
forward(55);
penup();
forward(30);
pendown();
}
void m(){
left(90);
forward(110);
right(150);
forward(55);
left(120);
forward(55);
left(30);
backward(110);
right(90);
penup();
forward(30);
pendown();
}
void n(){
left(90);
forward(110);
left(35);
backward(130);
right(35);
forward(110);
penup();
backward(110);
right(90);
forward(30);
pendown();
}
void o(){
penup();
left(90);
forward(80);
pendown();
backward(60);
int a = 1;
while (a <= 90){
backward(1);
left(2);
a++;
}
backward(60);
int b = 1;
while (b <= 90){
backward(1);
left(2);
b++;
}
penup();
right(135);
forward(110);
left(53);
pendown();
}
void p(){
left(90);
forward(110);
right(70);
int a = 1;
while (a<=120) {
forward(1);
right(2);
a++;
}
penup();
left(150);
forward(70);
left(70);
forward(30);
pendown();
}
void q(){
penup();
left(90);
forward(80);
pendown();
backward(60);
int a = 1;
while (a <= 90) {
backward(1);
left(2);
a++;
}
backward(60);
int b = 1;
while (b <= 90) {
backward(1);
left(2);
b++;
}
penup();
right(155);
forward(55);
pendown();
forward(60);
penup();
left(145);
forward(20);
right(80);
forward(25);
pendown();
}
void r(){
left(90);
forward(110);
right(70);
int a = 1;
while (a<=120) {
forward(1);
right(2);
a++;
}
left(150);
forward(70);
penup();
left(70);
forward(30);
pendown();
}
void s(){
penup();
left(90);
forward(20);
pendown();
int a = 1;
while (a<=120) {
left(2.2);
backward(1);
a++;
}
backward(30);
int b = 1;
while (b<=120) {
backward(1);
right(2.2);
b++;
}
penup();
backward(85);
right(90);
forward(30);
pendown();
}
void t(){
penup();
forward(40);
left(90);
pendown();
forward(110);
penup();
left(90);
forward(40);
pendown();
backward(80);
penup();
right(90);
backward(110);
right(90);
forward(30);
pendown();
}
void u(){
penup();
left(90);
forward(110);
pendown();
backward(80);
int a = 1;
while (a <= 90) {
backward(1);
left(2);
a++;
}
backward(80);
penup();
left(10);
forward(108);
left(80);
pendown();
}
void v(){
penup();
left(90);
forward(110);
left(20);
pendown();
backward(117);
right(35);
forward(117);
penup();
left(20);
backward(110);
right(90);
forward(30);
pendown();
}
void w(){
penup();
left(90);
forward(110);
left(10);
pendown();
backward(117);
right(25);
forward(60);
penup();
left(30);
pendown();
backward(60);
right(25);
forward(117);
penup();
left(10);
backward(110);
right(90);
forward(30);
pendown();
}
void x(){
left(75);
forward(114);
penup();
left(105);
forward(30);
right(75);
pendown();
backward(114);
penup();
right(105);
forward(30);
pendown();
}
void y(){
left(75);
forward(114);
penup();
left(105);
forward(30);
right(75);
pendown();
backward(57);
penup();
backward(57);
right(105);
forward(30);
pendown();
}
void z(){
penup();
forward(80);
pendown();
backward(80);
left(54);
forward(136);
right(54);
backward(80);
penup();
right(54);
forward(136);
left(54);
forward(30);
pendown();
}
void circle(){
int a = 1;
while (a<=449) {
forward(1);
right(1);
a++;
}
}
void space(){
penup();
forward(60);
pendown();
}
void drawName(String name) {
for (int i = 0; i < name.length(); i++) {
char letter = tolower(name[i]);
switch (letter) {
case 'a':
A();
break;
case 'b':
B();
break;
case 'c':
c();
break;
case 'd':
d();
break;
case 'e':
e();
break;
case 'f':
f();
break;
case 'g':
g();
break;
case 'h':
h();
break;
case 'i':
I();
break;
case 'j':
j();
break;
case 'k':
k();
break;
case 'l':
l();
break;
case 'm':
m();
break;
case 'n':
n();
break;
case 'o':
o();
break;
case 'p':
p();
break;
case 'q':
q();
break;
case 'r':
r();
break;
case 's':
s();
break;
case 't':
t();
break;
case 'u':
u();
break;
case 'v':
v();
break;
case 'w':
w();
break;
case 'x':
x();
break;
case 'y':
y();
break;
case 'z':
z();
break;
case ' ':
space();
break;
default:
Serial.println("Error! Please enter alphabets only.");
break;
}
}
delay(500); // Optional delay between letters
}
Hello Everyone!
Actually I was working on a project which can write letters.
Everything is done but the problem is that I want to access serial monitor through wifi so that I can input letters wirelessly. For this I want to use web serial but I don't know how to do this. I am using ESP8266 for the same. Kindly help, we are just 14 years and have started these things recently so we don't know much and hence I request you to kindly help us.