Updated code, part of it stops?

hi guys me again so my project is at the point its very nearly done, im connecting to a bluetooth module to the arduino to change a delay value, the problem is when i add this :

  if(BT.available()){
    bt = BT.readString();
  delay(400);//Small delay between each data send    
  }
  Serial.println(bt);
 
  while(!BT.available());

to my loop it brake this part of the code, as it it wont read the delay

 if (digitalRead(switchPin) == LOW){

// millis = (40); 
   //                        delay(bt.toInt());
delay(40);    //debounce, if it becomes a problem I usually use a counter414
      startTime = millis();    //get the start time before entering the loop 


  
      while( digitalRead(switchPin) == LOW && millis() < (startTime + 1000)) {   
         digitalWrite(MOSFET, HIGH);
//          bt = BT.readString();
  //        Serial.println(bt);
        }
        
      digitalWrite(MOSFET, LOW);   //this is the first thing it will do after exiting the while loop

also im trying to get it so if the switchpin is pressed and held i can still use another button to Write to the MOSFET. at the moment if i hold the switchpin down it ignores that im pressing

"SwitchPinAUTO"

for the moment i have removed the code for the switch as i was trying to sort out the first problem im having.

i have made the android app and it can read and send data. would it be a good idea to put it into a Void then read that from the loop?

here is my full code:

//////////////////////////////////////////// This one is the newest one to try /////////////////////////////////////////////
int MOSFET = A0; // LED on pin 5
int mosfet2 = A0;
int switchPin = A5; // momentary switch on 13, other side connected to ground
int SwitchPinAUTO = A1;
int startTime = millis;
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1); 
String bt = "";


/////////////////////////////////////////////////////////////////////////////////////////
void setup()

{

BT.begin(9600);
Serial.begin(9600);
bt = (40);

pinMode(MOSFET, OUTPUT);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // turn on pullup resistor
}
/////////////////////////////////////////////////////////////////////////////////////////
void loop(){
//Serial.println(bt);
//Serial.read();
{






  
////////////////////////// This Brakes my code below////////////////////////////
 /*
  
  if(BT.available()){
    bt = BT.readString();
  delay(400);//Small delay between each data send    
  }
  Serial.println(bt);
 
  while(!BT.available()); 
  */



  
  //////////////////////////////////////////////////////0
}



//Bluetooth.readBytes((char*)&number, sizeof(number));



 if (digitalRead(switchPin) == LOW){

// millis = (40); 
   //                        delay(bt.toInt());
delay(40);    //debounce, if it becomes a problem I usually use a counter414
      startTime = millis();    //get the start time before entering the loop 


  
      while( digitalRead(switchPin) == LOW && millis() < (startTime + 1000)) {   
         digitalWrite(MOSFET, HIGH);
//          bt = BT.readString();
  //        Serial.println(bt);
        }
        
      digitalWrite(MOSFET, LOW);   //this is the first thing it will do after exiting the while loop 
     
  
}
      
  
    
    }


/////////////////////////////////////////////////////////////////////////////////////////
  void callMillis(unsigned long x)
{
  unsigned long presentMillis = millis();
  while(millis() - presentMillis != x)
  {
/////////////////////////////////////////////////////////////////////////////////////////
  }
}
  if(BT.available()){
    bt = BT.readString();
  delay(400);//Small delay between each data send   
  }
  Serial.println(bt);
 
  while(!BT.available());

won't the above code hang if there is no serial input (that while statement)

Also do things like int startTime = millis(); instead of int startTime = millis; and not name variables // millis = (40); instead try // myMillis = 40; and, please, look this over Demonstration code for several things at the same time - Project Guidance - Arduino Forum

int startTime = millis()

unsigned long

unsigned lon startTime = millis()

Thanks for your help guys, at the moment its working, kind of. now iv managed to set the delay with the phone and it updates it, still having the problem after a while the code just stops working. is the unsigned long fix this?

im guessing its a runtime error???

Volanaro:
Thanks for your help guys, at the moment its working, kind of. now iv managed to set the delay with the phone and it updates it, still having the problem after a while the code just stops working. is the unsigned long fix this?

im guessing its a runtime error???

Could be, Could be. I have a habit of using uint64_t, overflows in 100+ years, for the ESP32 when I use time difference delays. My bad for not presenting using unsigned long with an Arduino Uno, an 8 bit machine. Unsigned Long will better able to hold/contain the millis() values.

So when you write your code stops, how do you know? Add in a few prints, for troubleshooting, to see where and when the code may be stopping.

when i press the switch pin it would make the motor spin for the specified time the "delay" after a while of pressing it it will ignore the delay and just turn the motor intill i lift of the button if you know what i mean?

here is the updated code, can you see anything or could make it cleaner? maybe its my lack of arduino code experience?

//////////////////////////////////////////// This one is the newest one to try /////////////////////////////////////////////
int MOSFET = A0; // LED on pin 5
int mosfet2 = A0;
int switchPin = A5; // momentary switch on 13, other side connected to ground
int SwitchPinAUTO = A1;
int switchPin2 = A4;
int startTime = millis;
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1);
String bt = "";
String bt1 = "";
/////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
  BT.begin(9600);
  Serial.begin(9600);
  bt = (40);

  pinMode(MOSFET, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH); // turn on pullup resistor
}
/////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  //Serial.println(bt);
  //Serial.read();
  bt1 == bt;
  ////////////////////////// This Brakes my code below////////////////////////////
  /*
     if(BT.available()){
     bt = BT.readString();
    delay(4000);//Small delay between each data send
    }
    Serial.println(bt);
    // while(!BT.available());
  */
  //////////////////////////////////////////////////////0
  //Bluetooth.readBytes((char*)&number, sizeof(number));
  ///if (digitalRead(switchPin2) == LOW){
  // digitalWrite(MOSFET, HIGH);
  // digitalWrite(MOSFET, LOW);
  //}
  if (digitalRead(switchPin) == LOW) {
    //if (digitalRead(switchPin) == LOW){
    // millis = (40);
   // millis(bt.toInt());
    delay(bt.toInt());
    //delay(40);    //debounce, if it becomes a problem I usually use a counter414
    startTime = millis();    //get the start time before entering the loop
    while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000)) {
      digitalWrite(MOSFET, HIGH);
      // Serial.println(bt);
      //        Serial.println(bt);
    }
    digitalWrite(MOSFET, LOW);   //this is the first thing it will do after exiting the while loop
  }
  if (digitalRead (A1) == LOW && BT.available() ) {
    bt = BT.readString();
    //  delay(40);
    //Serial.println(bt1);

    Serial.println(bt);
  }
}
/////////////////////////////////////////////////////////////////////////////////////////
void callMillis(unsigned long x)
{
  unsigned long presentMillis = millis();
  while (millis() - presentMillis != x)
  {
    /////////////////////////////////////////////////////////////////////////////////////////
  }
}
int MOSFET = A0; // LED on pin 5
int mosfet2 = A0;

Ignoring the silly comment...why two different names for pin A0? You never use either in the following code so they can go.

You have so many comments that are meaningless and so much code commented out that I lost track (and almost all interest). Also you have a function callMillis() that is never called (and would stop the program dead if you did call it), So that can go. And startTime is STILL not an unsigned long. Plenty of opportunities to clean your code up a bit.

Steve

so iv cleaned up the code a bit and now it seems that its working properly, thank you for pointing me in the right direction. and im learning the arduino code that's why there was loads of comments haha. the led pin comment for example was when i was using a led to test the output here is the updated code so far.

//////////////////////////////////////////// This one is the newest one to try /////////////////////////////////////////////
int MOSFET = A0; // Mosfeton pin A0
int switchPin = A5; // momentary switch on 13, other side connected to ground
int switchPin2 = A4;
//int startTime = millis;
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1);
String bt = "";

/////////////////////////////////////////////////////////////////////////////////////////
unsigned long startTime = millis();
void setup()
{
  BT.begin(9600);
  Serial.begin(9600);
  bt = (40);
  pinMode(MOSFET, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH); // turn on pullup resistor
}
/////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  ////////////////////////// This Brakes my code below////////////////////////////

  if (digitalRead(switchPin) == LOW) {
    delay(bt.toInt());//debounce, if it becomes a problem I usually use a counter414
    startTime = millis();    //get the start time before entering the loop
    while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000)) {
      digitalWrite(MOSFET, HIGH);
    }
    digitalWrite(MOSFET, LOW);   //this is the first thing it will do after exiting the while loop
  }
  if (digitalRead (A1) == LOW && BT.available() ) {
    bt = BT.readString();
    Serial.println(bt);
  }
}
/////////////////////////////////////////////////////////////////////////////////////////
void callMillis(unsigned long x)
{
  unsigned long presentMillis = millis();
  while (millis() - presentMillis != x)
  {
    /////////////////////////////////////////////////////////////////////////////////////////
  }
}

so i added another switch to pin A4 and added the code:

 if (digitalRead(switchPin) == LOW && digitalRead(switchPin2)== LOW) {
 digitalWrite(MOSFET, HIGH);
}

but if i keep t"switchPin" pressed and then press "switchPin2" it dose nothing how would the best way to go about this?

Current code:

//////////////////////////////////////////// This one is the newest one to try /////////////////////////////////////////////
int MOSFET = A0; // Mosfeton pin A0
int switchPin = A5; // momentary switch on 13, other side connected to ground
int switchPin2 = A4;
//int startTime = millis;
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1);
String bt = "";

/////////////////////////////////////////////////////////////////////////////////////////
unsigned long startTime = millis();
void setup()
{
  BT.begin(9600);
  Serial.begin(9600);
  bt = (40);
  pinMode(MOSFET, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH); // turn on pullup resistor
}
/////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  ////////////////////////// This Brakes my code below////////////////////////////
 if (digitalRead(switchPin) == LOW && digitalRead(switchPin2)== LOW) {
 digitalWrite(MOSFET, HIGH);
  
}


  if (digitalRead(switchPin) == LOW) {
    delay(bt.toInt());//debounce, if it becomes a problem I usually use a counter414
    startTime = millis();    //get the start time before entering the loop
    while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000)) {
      digitalWrite(MOSFET, HIGH);
    }
    digitalWrite(MOSFET, LOW);   //this is the first thing it will do after exiting the while loop
  }
  if (digitalRead (A1) == LOW && BT.available() ) {
    bt = BT.readString();
    Serial.println(bt);
  }
}
/////////////////////////////////////////////////////////////////////////////////////////
void callMillis(unsigned long x)
{
  unsigned long presentMillis = millis();
  while (millis() - presentMillis != x)
  {
    /////////////////////////////////////////////////////////////////////////////////////////
  }
}
  while (millis() - presentMillis != x)

Sometimes milllis() counts by 2 instead of 1. If that skips over (millis() - presentMillis == x) then your loop will never end. Change it to:

  while (millis() - presentMillis <= x)
SoftwareSerial BT(0, 1);

What Arduino are you using where Pin 0 and Pin 1 are not used by Serial?!?

Why write a 'callMillis()' function that does the same thing as 'delay()'?

Why on earth are you using a String (bt) to store the integer debounce time?!? WHY is the debounce time something you receive from Bluetooth?!?

What is connected to A1? Why is pinMode not set for switchPin2 or A1?

iv manged to get the code to work, the only problem im having now is keeping the switchpin down while pressing another switch to send data to the same output pin if that make sense?

i have to convert bt to be able to read the value, im sending a "text" from a textbox from my phone to the arduino A1 is connected to a external mosfet

Volanaro:
iv manged to get the code to work, the only problem im having now is keeping the switchpin down while pressing another switch to send data to the same output pin if that make sense?

Doesn't make sense to me but if you want to use another input pin and turn on the pull-up resistor you should call pinMode() and digitalWrite() for it (or use pinMode(pin, INPUT_PULLUP) to do both in one step).

Volanaro:
i have to convert bt to be able to read the value, im sending a "text" from a textbox from my phone to the arduino

WHY are you sending a debounce delay from a phone app?!?

Volanaro:
A1 is connected to a external mosfet

So A0 and A1 are connected to two different MOSFETs and A0 has been given the name 'MOSFET' and A1 has not been given a name? The program would be easier to understand if you named both pins and the names said what the MOSFETs are controlling. You don't seem to have a pinMode() for A1.

Sorry iv updated my Code so you might be able to read it a bit better.
i need it so when i press and hold switch1 down & then press Switch2 to Start the motor While both are pressed

updated code:

//============ Mosfet Arduino ============ 
#include "OneButton.h"
int MOSFET = A0;                   // Mosfet pin A0
int switchPin = A5;               // Switch 1
int switchPin2 = A4;             //Switch   2
//int startTime = millis;
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1);
String bt = "";
unsigned long startTime = millis();


//============  Start Of Void Setup ============ 
void setup()
{
  BT.begin(9600);
  Serial.begin(9600);
  bt = (40);
  pinMode(MOSFET, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);
}
//============  Void End ============ 
//============  Start Of Void Loop ============ 
void loop() {

/*
 * Add code to Send Data While switchpin is Held down and Switchpin2 is pressed
 */
//============   Switchpin pressed ============ 
  if (digitalRead(switchPin) == LOW) {
    delay(bt.toInt());                                                         // How Long to Keep the motor running for
    startTime = millis();                                                     //get the start time before entering the loop
    while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000)) {
      digitalWrite(MOSFET, HIGH);
    }
    digitalWrite(MOSFET, LOW);                                               //this is the first thing it will do after exiting the while loop
  }
  if (digitalRead (A1) == LOW && BT.available() ) {
    bt = BT.readString();
    Serial.println(bt);
  }
}
//============   end of Switchpin  ============ 

//============  Void Loop End ============ 

//============  Void callMillis ============ 
void callMillis(unsigned long x)
{
  unsigned long presentMillis = millis();
  while (millis() - presentMillis != x)
  {
//============ End Void callmillis ============ 
  }
}

sorry about all the wires overlapping but this is the best i could do to try and show you, and the mosfet unit im using

So you want switchPin and switchPin2 both to be pressed to start the motor. Do you think it might be helpful if you actually read switchPin2 at some point in your code? When will the motor stop?

Currently switchPin on it's own starts the motor. So what exactly does switchPin2 add to the story?

Oh and what is connected to pin A1. I can't see that on your picture.

Steve

ok so an update, i managed to get it to kind of work, if i press the first button then press the second button within say 500ms it will work i.e it will make the motor turn on on while the first and second button is pressed. but if i press the first button then wait say 1 or 2 seconds, then press the second button it dose nothing?

i added and changed switch2 to : pinMode(MOSFET, OUTPUT); to get this far is this is my current code, ignore the commented code i was testing it, that did not work. what do i have to do to get this to work right? im %99 done with this project just this bit left to do, iv also finalised my Android app to connect and talk to the arduino that's working fine, i need it so when i press the first button i can leave that pressed in how ever long i wanted then if i press the second button while the first one is still pressed.

here is my updated code:

//============ Mosfet Arduino ============ 
#include "OneButton.h"
int MOSFET = A0;                   // Mosfet pin A0

int switchPin = A5;               // Switch 1
int switchPin2 = A4;             //Switch   2
//int startTime = millis;
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1);
String bt = "";
unsigned long startTime = millis();
bool T1 = 0;

//============  Start Of Void Setup ============ 
void setup()
{
  BT.begin(9600);
  Serial.begin(9600);
  bt = (40);
  pinMode(MOSFET, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);
  
    pinMode(switchPin2, OUTPUT`);
  digitalWrite(switchPin2, HIGH);
}
//============  Void End ============ 
//============  Start Of Void Loop ============ 
void loop() {

/*
 * Add code to Send Data While switchpin is Held down and Switchpin2 is pressed

 if (digitalRead(switchPin2) == LOW) {
    delay(bt.toInt());                                                         // How Long to Keep the motor running for
    startTime = millis();                                                     //get the start time before entering the loop
    while (digitalRead(switchPin) == LOW && digitalRead(switchPin2)== LOW);  {
      digitalWrite(MOSFET, HIGH);
    }
    digitalWrite(MOSFET, LOW);                                               //this is the first thing it will do after exiting the while loop
  }
  */


  
//============   Switchpin pressed ============ 
  if (digitalRead(switchPin) == LOW) {
    
    delay(bt.toInt());                                                         // How Long to Keep the motor running for
    startTime = millis();   
    //get the start time before entering the loop
  
    while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000) && (T1 = 1)) {
      digitalWrite(MOSFET, HIGH );  
      
    }
  digitalWrite(MOSFET, LOW);
  
  //this is the first thing it will do after exiting the while loop

  }

  
  if (digitalRead (A1) == LOW && BT.available() ) {
    bt = BT.readString();
    Serial.println(bt);
  }

   // digitalWrite(MOSFET, LOW); 
}
//============   end of Switchpin  ============ 

//============  Void Loop End ============ 

//============  Void callMillis ============ 
void callMillis(unsigned long x)
{
  unsigned long presentMillis = millis();
  while (millis() - presentMillis != x)
  {
//============ End Void callmillis ============ 
  }
}

Does this sketch do what you wanted?
If not, in what way is it acting different from what you wanted?

//============ Mosfet Arduino ============


// Pin declarations
const byte MOSFET_DOPin = A0;         // Motor control MOSFET
const byte Button1_DIPin = A5;
const byte Button2_DIPin = A4;


//============  Start Of setup() ============
void setup()
{
  Serial.begin(9600);
  
  pinMode(MOSFET_DOPin, OUTPUT);
  pinMode(Button1_DIPin, INPUT_PULLUP);
  pinMode(Button2_DIPin, INPUT_PULLUP);
}
//============  End od setup() ============


//============  Start of loop() ============
void loop()
{
  static unsigned long delayValue = 40;  // How Long to Keep the motor running


  // If a new delay value has arrived from Bluetooth, read it.
  if (Serial.available())
  {
    String s = Serial.readString();
    delayValue = s.toInt();
    Serial.println(delayValue);
  }


  // If both buttons are pressed, run the motor for the selected number of milliseconds
  if ((digitalRead(Button1_DIPin) == LOW) && (digitalRead(Button1_DIPin) == LOW))
  {
    digitalWrite(MOSFET_DOPin, HIGH );  // Start motor
    delay(delayValue);
    digitalWrite(MOSFET_DOPin, LOW );  // Stop motor
  }
}
//============  End of loop() ============

with the code you posted it dose not work, i need it so if "button1" is pressed the turn motor for X amount of time, then if "button1" is pressed and held down then "button2" is pressed then turn motor on intill "button2" is released
With my current code it dose what i want it to but if i press button 1 then wait before i press button 2 it dose nothing, but if i press button1 then button 2 shortly after it works?

//============ Mosfet Arduino ============ 
#include "OneButton.h"
int MOSFET = A0;                   // Mosfet pin A0

int switchPin = A5;               // Switch 1
int switchPin2 = A4;             //Switch   2
//int startTime = millis;
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1);
String bt = "";
unsigned long startTime = millis();
bool T1 = 0;
//============  Start Of Void Setup ============ 
void setup()
{
  BT.begin(9600);
  Serial.begin(9600);
  bt = (40);
  pinMode(MOSFET, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);
  
    pinMode(switchPin2, OUTPUT);
  digitalWrite(switchPin2, HIGH);
}
//============  Void End ============ 
//============  Start Of Void Loop ============ 
void loop() {
 
  val = (analogRead(A3));
Serial.print(val);
if (val < 500)
{
  Serial.print("Empty");
}
else

{
Serial.print("Full");
}
delay(1000);
  
//============   Switchpin pressed ============ 
  if (digitalRead(switchPin) == LOW) {
    
    delay(bt.toInt());                                                         // How Long to Keep the motor running for
    startTime = millis();   
    //get the start time before entering the loop
  
    while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000) && (T1 = 1)) {
      digitalWrite(MOSFET, HIGH );  
      
    }
  digitalWrite(MOSFET, LOW);
  
  //this is the first thing it will do after exiting the while loop

  }

  
  if (digitalRead (A1) == LOW && BT.available() ) {
    bt = BT.readString();
    Serial.println(bt);
  }

   // digitalWrite(MOSFET, LOW); 
}
//============   end of Switchpin  ============ 

//============  Void Loop End ============ 

//============  Void callMillis ============ 
void callMillis(unsigned long x)
{
  unsigned long presentMillis = millis();
  while (millis() - presentMillis != x)
  {
//============ End Void callmillis ============ 
  }
}