So, with the help of @uskiara I built an app mit app that controls my dog's door, controlls NEMA 17 stepper motor and electromagnet via bt module hc-05. Also with the help of microswitch I change the state of the door(closed/open) based on the digital state.
All of it worked fine until few days ago when I lost the microswitch effect, it just didn't want to change state(I am using internal pull up resistor and I realized my voltage between C pin and NO of the microswitch was 0,4V, whereas when I connect only arduino to the microswitch I had 5V.
Also, my app freeze after I push a button and also, I receive some gibberish data on the serial monitor since yesterday.
Below are my cc code, mit app blocks and schematic I made. I will post serial monitor now.
#include <SoftwareSerial.h>
#define TxPin 6
#define RxPin 5
#define el_magnet 7
#define command 3
#define debug
bool el_magON= false;
unsigned long prethodno_vrijeme=0;
const long interval=15000;
SoftwareSerial BTSerial(RxPin,TxPin);
char state = ' ';
const int stepPin = 4;
const int directionPin = 2;
int buttonState = LOW;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
//-----------------------------------------------------------------------------------------------
void zatvori()
{
digitalWrite(directionPin,LOW);
StepperActuate();
}
//-----------------------------------------------------------------------------------------------
void otvori()
{
digitalWrite(directionPin,HIGH);
StepperActuate();
}
//-----------------------------------------------------------------------------------------------
void StepperActuate()
{
for(int x = 0; x < 50; x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
//===============================================================================================
void setup()
{
pinMode(el_magnet,OUTPUT);
digitalWrite(el_magnet,LOW);
pinMode(command,INPUT_PULLUP);
pinMode(stepPin,OUTPUT);
pinMode(directionPin,OUTPUT);
BTSerial.begin(9600);
Serial.begin(9600);
}
//===============================================================================================
void loop()
{
int reading = digitalRead(command);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
BTSerial.println("1");
} else {
BTSerial.println("0");
}
}
}
lastButtonState = reading;
static char character = 'A';
static unsigned long lastPrintTime = 0;
unsigned long currentTime = millis();
if (currentTime - lastPrintTime >= 90000) {
BTSerial.println(character);
lastPrintTime = currentTime;
}
if (BTSerial.available())
{
state = BTSerial.read();
#ifdef debug
Serial.print("Received : ");
Serial.println(state);
#endif
if(state=='2')
{
otvori();
}
if(state=='3')
{
zatvori();
}
if(state=='4') {
digitalWrite(el_magnet,LOW);
el_magON=false;
}
if(state=='5') {
if (!el_magON) {
digitalWrite(el_magnet, HIGH);
el_magON=true;
prethodno_vrijeme=millis();
}
}
}
unsigned long trenutno_vrijeme=millis();
if (el_magON && (trenutno_vrijeme-prethodno_vrijeme>=interval)){
digitalWrite(el_magnet, LOW);
el_magON=false;
}
}
I know there is a lot going on here but I really have no idea why these weird behaviour happens.