I tried to make a program which can be operated by smart phone. I set the program to run motor which is running 5 times motor and stopped it. also have to stop during the operation when send the "off" order. But the problem when i send the "off" order during the while process did not stop it. Someone teach me how to programing it. I am a very beginner to do program. Help me.
#include <Wire.h>
int ledPin = 13;
int motorPin = 9;
int ontime = 300;
int offtime = 600;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode (motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}
void loop() {
// put your setup code here, to run once:
int j=0;
while (Serial.available()) {
delay (3);
char c = Serial.read();
readString += c;
}
if (readString.length() > 0) {
Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
delay(ontime);
while (j<=5){
j= j+1;
if (readString=="off"){
j=0;}
for (int i = 0; i<=255; i++)
{
analogWrite(motorPin,i);
delay(20);
}
delay(2500);
for(int i=255; i>=0; i--)
{
analogWrite(motorPin,i);
delay(20);
}
}
if (readString == "off")
{
analogWrite(motorPin,LOW);
digitalWrite(ledPin, LOW);
}
}
}
readString = "";
}