Hello everyone
I am trying to get a pice of code to work but i am unable to and i ask for your help if you can... ![]()
The Project
I have a pice of code the runs as:
- it's a countdown timer that when started, turns a relay on and when it get's to "0" e stops and turns off the relay.
-what i wanted is to is to add a stepper to the mix, so when the countdown gets to "0" all stops.
the realety ... when i add the stepper, the cowntdowd stats to fail in so cases, the stepper keeps starting and stoping, the relay does not turn off and ... nothing works...
I am using a board from DeeksRobot based on the L293D chip to control the stepper
Can anyone inlight me in what i am doing wrong...
Thank you
#include <Wire.h>
#include <Adafruit_SH1106.h>
#include <Adafruit_GFX.h>
#include <Stepper.h>
#define OLED_ADDR 0x3C
// 10000ul = 10 sec
// 30250ul = 30 sec
// 60500ul = 1 min
// 302500ul = 5 min
// 605000ul = 10 min
// 907500ul = 15 min
#define COUNTDOWN_TIME 10000ul // Set timer
#define pinRele 7 //Set relay Pin
#define nivelRele HIGH //Relay logic
// Define number of steps per rotation:
const int stepsPerRevolution = 2048;
// Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 8, 9, 10, 11);
char
szString[20];
byte
mins, secs;
unsigned long
timeTemp,
timeNow,
timeStart,
timeElapsed,
timeLeft;
#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);
// difine buzzer
const int buzzer = 4; //buzzer to arduino pin 9
void setup()
{
// Set the speed to 1 or 2 for very slow speed:
// Set the speed to 15 for very high speed:
myStepper.setSpeed(2);
// RELAY SETTINGS
pinMode(pinRele, OUTPUT);
digitalWrite(pinRele, !nivelRele);
// initialize and clear display
display.begin(SH1106_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);
Serial.begin(9600);
timeStart = millis();
mins = 1;
secs = 1;
pinMode(buzzer, OUTPUT); // Set buzzer - pin 4 as an output
}//setup
void DoCountdown()
{
static unsigned long
lastTimeNow = 0;
static byte
lastsecs = 1;
timeNow = millis();
timeElapsed = timeNow - timeStart;
if( mins == 0 && secs == 0 )
return;
timeLeft = COUNTDOWN_TIME - timeElapsed;
mins = (byte)(timeLeft / 60000ul);
timeTemp = timeLeft - (mins * 60000);
secs = (byte)(timeTemp / 1000ul);
timeTemp = timeTemp - (secs * 1000ul);
if( mins == 0 && secs == 0 )
{
display.clearDisplay();
sprintf( szString, "DONE" );
Serial.println( szString );
display.setCursor( 25, 20 );
display.print( szString );
display.display();
time_ended();
return;
}//if
else if( secs != lastsecs )
{
lastsecs = secs;
sprintf( szString, "%02d:%02d", mins, secs );
Serial.println( szString );
display.setCursor( 20, 20 );
display.clearDisplay();
display.print( szString );
display.display();
}//if
}
void relay_action(){
if ( mins == 0 && secs == 0 ) {
digitalWrite(pinRele, nivelRele);
} else {
digitalWrite(pinRele, !nivelRele);
}
}
void stepper_motor() {
// Step one revolution in one direction:
myStepper.step(stepsPerRevolution);
}
void time_ended() {
tone(buzzer,5000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec // wait for a second
tone(buzzer, 5000); // Stop sound...
delay(1000); // ...for 1sec // wait for a second
noTone(buzzer);
delay(1000); // ...for 1sec // wait for a second
tone(buzzer, 5000); // Stop sound...
delay(1000); // ...for 1sec // wait for a second
noTone(buzzer);
}
void loop()//DoCountdown
{
DoCountdown();
relay_action();
stepper_motor();
return;
}//loop