Hi all,
Been working on some code that would take an Int or string(not sure which one it is)
and add it to another int or string.
for example:
int Count = 0
Count = Total accumulated // Max 200
Count = Count + 20 //random number between 0-200
when count = 200
reset count to 0
and run some one off code.
I'v been getting some advice from a different post but im guessing there asleep now, I was asked to try and work this out for my self, Its been 6 hours now and im tired, and I would really like to get this done in the next hour or at least workout why my code dont work. Hopefully you can make sense of it.
I'v commented the new code like this "// NEW CODE" so you can find it easily.
Best Regards
Dave
#include <MobaTools.h> // Stepper-Lib
#include <L298N.h> // Dc Motor Driver-Lib
#include <LiquidCrystal.h> // LCD Display-Lib
//const int rs = 5, en = 6, d4 = A1, d5 = A2, d6 = A3, d7 = A4;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// defines pin numbers for DC Motors
#define ENA 11 // Take-Up Reel // PWM
#define IN1 8
#define IN2 12
#define ENB 3 // ReWind Reel // PWM
#define IN3 4
#define IN4 7
#define DC_SPEED 255 //Dc Motor Default Speed. Min 40 to get the reel spinning.
#define MOTOR_STEPS 200 // 1/4 Step but i think library only support full or half
#define MICROSTEPS 4
#define DIR 9
#define STEP 13
#define SLEEP 2
// Attaches defines to stepper
MoToStepper stepper(MOTOR_STEPS, STEPDIR);
// Create one motor instance
L298N RightMotor (ENA, IN1, IN2); // Take up Reel
//L298N LeftMotor (ENB, IN3, IN4); // Rewind Reel
////////////////////////////////////////////////////////////////////////////////////
int rotationSpeed1;
char termChar = '\n';
bool rotResult = false;
//long int g = 0;
//long int sum;
/////////////////////////////////////////////////////////////////////
void setup() {
// Used to display information
Serial.begin(9600);
// Wait for Serial Monitor to be opened
while (!Serial)
{
//do nothing
}
//Serial.write(0x0C);
// serialCLS();
//////////////////////////////////////////////////////////////////////////////////////
pinMode(SLEEP, OUTPUT);
digitalWrite(SLEEP, HIGH);
// set up the LCD's number of columns and rows:
//lcd.begin(16, 2);
delay(5000); // Gives me time to clear the serial monitor! //
//lcd.setCursor(0, 0);
Serial.println("+-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+");
Serial.println("|C| |i| |n| |e| |S| |c| |a| |n| |n| |e| |r|");
Serial.println("+-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+");
Serial.println("");
Serial.println("");
Serial.println("*********************");
Serial.println("** Loading Configs **");
//lcd.print("Loading Configs");
//lcd.setCursor(0, 1);
Serial.println("** Stepper Driver **");
//lcd.print("Stepper Driver");
// Stepper Motor(s)
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
//delay(3500);
//lcd.clear();
// DC Motor(s)
//lcd.setCursor(0, 0);
//lcd.print("Loading Configs");
//lcd.setCursor(0, 1);
Serial.println("** DC Motors **");
//lcd.print("DC Motors");
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Turn off motors - Initial state
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
//delay(3500);
//lcd.clear();
//lcd.setCursor(0, 0);
//lcd.print("Loading Complete!");
//delay(3500);
//lcd.clear();
//lcd.setCursor(0, 0);
Serial.println("** Ready **");
Serial.println("*********************");
//lcd.print("Ready");
//delay(500);
stepper.attach(STEP, DIR); //STEP=Pin, DIR=Pin
}
void loop()
{
while (!Serial.available());
String incommingString = Serial.readStringUntil('\n');
int len;
len = incommingString.length();
//lcd.clear();
//lcd.setCursor(0, 0);
Serial.print("\nincommingString = "); // for IDE to test
Serial.println(incommingString); // for IDE to test
Serial.println();
//lcd.print("Incomming String "); // for IDE to test
//lcd.setCursor(2, 1);
//lcd.print(incommingString); // for IDE to test
String incommingStr = incommingString.substring(len - 12, len); // truncate to the correct length
if (incommingStr.length() != 12)
{
rotResult = false; // rotation was not successfull
}
else
{
Serial.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
String mode = incommingStr.substring(0, 1); // 1 byte
Serial.print("mode = ");
Serial.print(mode);
Serial.println(" (F)ull Step, (H)alf Step (M)icro Step 1/4");
delay(10);
String dir = incommingStr.substring(1, 2); // 1 byte
Serial.print("direction = ");
Serial.print(dir);
Serial.println(" (F)orwards, (B)ackwards");
String noOfSteps = incommingStr.substring(2, 6); //4 bytes
Serial.print("Number Of Steps = ");
Serial.println(noOfSteps);
String rotationSpeed = incommingStr.substring(6, 10); //4 bytes
Serial.print("rotationSpeed = ");
Serial.println(rotationSpeed);
String sdelay = incommingStr.substring(10, 12); //2 bytes (Unused)
Serial.print("sdelay(not used) = ");
Serial.println(sdelay);
int rotationSpeed1 = rotationSpeed.toInt();
int noOfSteps1 = noOfSteps.toInt();
String sum2, g; // NEW CODE
int sdelay1 = sdelay.toInt();
Serial.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
/////////////////////////////////////////////////////////////////////////////
stepper.setSpeed(rotationSpeed1); //RPM
rotResult = true; // initialize rotation result to erronous
if (dir == "F")
{
stepper.doSteps(-noOfSteps1 * MICROSTEPS); // Moves Stepper Forwards
Serial.print("\nsum = "); // NEW CODE
sum2 = g + noOfSteps; // NEW CODE
Serial.print("\nsum = "); // NEW CODE
//Serial.println(countedSteps);
Serial.println();
/*
currentStep = stepper.readSteps();
Serial.println("Just After The Move ");
Serial.print("Steps From ReadSteps = ");
Serial.println(stepper.readSteps());
Serial.print("Step From Variable = ");
Serial.println(currentStep);
Serial.print("Step From Stepper Pos = ");
Serial.println(stepper.currentPosition());
*/
// if (S_Switch_State == HIGH)
{
delay(500); //////////////////////////////////
analogWrite(ENA, 0); //50 // Take Up Reel (Forward)
digitalWrite(IN1, LOW); //
digitalWrite(IN2, HIGH); // Winds excess film onto
//delay(noOfSteps1); // the reel.
delay(18);
analogWrite(ENA, 0); //////////////////////////////////
}
}
else
{
stepper.doSteps(noOfSteps1 * MICROSTEPS); // Moves Stepper Backwards
}
if (mode == "F")
{
//stepper.doSteps(noOfSteps1); // Moves Stepper Backwards
for (int i = 0; i < noOfSteps1; i++)
{
//tmc26XStepper.move();
delay(6);
}
}
if (mode == "H")
{
//stepper.doSteps(noOfSteps1 * MICROSTEPS); // Moves Stepper Backwards
for (int i = 0; i < noOfSteps1; i++)
{
//tmc26XStepper.step(noOfSteps1);
//tmc26XStepper.move();
delay(10);
}
}
if (mode == "M")
{
//stepper.doSteps(noOfSteps1 * MICROSTEPS + 200); // Moves Stepper Backwards
for (int i = 0; i < noOfSteps1; i++)
{
//tmc26XStepper.step(noOfSteps1);
//tmc26XStepper.move();
}
}
rotResult = true; // rotation was successfull
}
if (rotResult == true)
{
Serial.write("1111111111"); Serial.print(" Error Code "); Serial.println(); // rotation successfull
Serial.print("\nsum = "); // NEW CODE
/*
Serial.println("End The Of Loop ");
Serial.print("Steps From ReadSteps = ");
Serial.println(stepper.readSteps());
Serial.print("Step From Variable = ");
Serial.println(currentStep);
Serial.print("Step From Stepper Pos = ");
Serial.println(stepper.currentPosition());
*/
}
else
{
Serial.write("0000000000"); Serial.print(" Error Code "); Serial.println();// rotation failed
Serial.print("\nsum = "); // NEW CODE
}
}