Hi, I'm absolutely new to Arduino. I'm using an Arduino One board to drive a stepper motor. The project has to end in an useful watch winder. I want the winder to turn clock wise and counter clock wise with a break in between. This all by selected random numbers.
My succes so far is to have the motor spinning clock wise and counter clockwise, pause in between all by random.
As i want to read on a lcd I also have managed to display the turns in numbers and the breaks in seconds.
As cherry on the pie I want to add the total of the rotations and show the in the bottom line of the lcd. But here is my problem. I just cant make the random numbers to add.
I tried a for loop, but cant figure out what the upper limit is and the numbers just wont add.
Please see my code i (appologise for the coding. It isd my first serious atempt) - but it works for what i wanted so far.
/*
Allegro A3967 based EasyStepper board
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//I2C pins declaration
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int dirPin = 2;
int stepperPin = 3;
int smEnablePin = 7;
long randnummer;
long randnummerccw;
long kies;
int stop;
int stopccw;
float rot;
float rotccw;
int sec;
int secccw;
int mC;
void setup() {
lcd.begin(16,2);//Defining 16 columns and 2 rows of lcd display
lcd.backlight();//To Power ON the back light
//lcd.backlight();// To Power OFF the back light
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
pinMode(smEnablePin, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void step(boolean dir,int steps){
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++){
digitalWrite(stepperPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepperPin, LOW);
delayMicroseconds(1000);
}
mC=0;
}
void loop(){
//randnummer = 0;
//randnummerccw = 0;
//mC=0;
kies = random(1,10000);
Serial.println((String)"kies waarde:" +kies);
if (kies >= 5000){
randnummer = random(0, 5);
rot = randnummer * 1600;
lcd.setCursor(0,0); //Defining positon to write from first row,first column .
lcd.print("cw rotatie: ");
lcd.print (randnummer);
//lcd.print((String)"cw rotatie: " +rot);
//lcd.print((String)"cw steps: " + randnummer);
//Serial.println((String)"cw steps:" +randnummer);
step(true,rot);
//if(random > 0){
//lcd.setCursor(6,1);
//lcd.print(random);
//delay (1000);
//random --;}
//lcd.clear();//Clean the screen
mC = mC+randnummer;
lcd.setCursor(0,1);
lcd.print(mC);
stop = random(0, 5);
lcd.setCursor(0,0); //Defining positon to write from second row,first column .
sec = stop;
lcd.print((String)"Wacht "+sec);
lcd.print (" sec ");
//Serial.println((String)"Ms stop:" + stop);
digitalWrite (smEnablePin, HIGH);
delay(stop * 1000);
digitalWrite (smEnablePin, LOW);
//lcd.clear();//Clean the screen
}
else
{
randnummerccw = random(0, 8);
rotccw = randnummerccw * 1600;
lcd.setCursor(0,0); //Defining positon to write from first row,first column .
lcd.print((String)"ccw rotatie: " +randnummerccw);
//lcd.print((String)"cw steps: " + randnummerccw);
//Serial.println((String)"ccw steps:" +randnummerccw);
step(false,rotccw);
//lcd.clear();//Clean the screen
stopccw = random(500, 10000);
lcd.setCursor(0,0); //Defining positon to write from second row,first column .
secccw = stopccw / 1000;
lcd.print((String)"Wacht "+secccw);
lcd.print (" sec ");
//Serial.println((String)"Ms stop:" + stop);
digitalWrite (smEnablePin, HIGH);
delay(stopccw);
digitalWrite (smEnablePin, LOW);
//delay(500);
//lcd.clear();//Clean the screen
}
}