Delayed pressed button

Thanks for the code void-basil it works great, I’ll look at adding my other code in for the arm,
Lee737 I’ll start another thread when I’m happier with my progress but it has been fun so far :smiley:
Thanks very much again and sorry again for being such a beginner :smiley:
Thanks again for all the help,
Phil.

I have no idea...

Here's my output, with the print lines 5s apart while the led is on:

setup() done
Press the button...  pressed
Press the button...  pressed
Press the button...

Is your button wired the right way:

// ***** wire the button from pin to ground *****
const byte button = 2;

EDIT OK now you make me look like a pillock... I posted a reply to your #20 which is very different now after the edit and my reply looks crazy...

No it wasn’t when I wrote the first message, my mistake, I edited the post when I found out, it all works perfect now, sorry and I’m sure everyone that knows you, knows your not a pillock :wink:

sorry

No harm done, but it might have been better to leave the post and add an "edit: ah it's working now" at the bottom, or wait the 5mins post again.
Question of course is, why didn't my code work for you originally or more to the point, do you know what you did to make it work in case it falls over again?

Yes I was originally using the button on the arduino and not a button on a breadboard, I figured out that the button on the arduino was a reset button and the code was starting over again, how stupid do I look now :smiley: hey everyone has to start somewhere, like I said it’s working perfectly now which I’m sure you knew it would, it looks pretty simple when you look at the code, thank you,
I have a lot to learn.

Anyhoo, I think your next steps should be to work with each of your components (horn, solenoid, brushed motor, stepper) one at a time and get your mind round how each works, outside of the specific context of your project.

Don't try to bolt them together until you're reasonably happy each works in isolation. (IMO, anyway)

Hmm that sound like a plan, I have my stepper motor code for the arm, I just need to add in a movement to stepper 2 for the wrist as stepper 1 is moving.

heres the code for that

// stepper motor code
const int stepPin = 3;
const int dirPin = 4;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}
void loop() {
  digitalWrite(dirPin,LOW); //HIGH clockwise, LOW anticlocwise
  for (int x = 0; x < 495; x++) { // loop for 495 steps
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2500);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2500);
  }
  delay(2000); // delay for 2 second
  digitalWrite(dirPin, HIGH); //HIGH clockwise, LOW anticlocwise
  for (int x = 0; x < 495; x++) { // loop for 495 steps
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2500);
  }
  delay(2000); // delay for 2 second
  
}

that's for it to move as in the youtube video, I need to add a second movement for the wrist from about 150 steps to 200 steps and the wrist would need to do around 80 steps.

just need to

:wink:

I see in that code you hand-crafted the stepper control in the code. It's way simpler to use a library in principle, but to control 2 at the same time make sure you use a non-blocking library. I have very little experience of steppers, but I think (not 100% sure) that Stepper.h is of a blocking type, and AccelStepper.h is not.

I believe I modified a stepper.h program from the library, ill have to look at Accelstepper.h but for now I will concentrate on changing your button code from an LED to a solenoid.

well ive made a bit of progress over the paste few days but now im a little confused, this is the program so far:-
1, wait fir button to be pressed
2, delay
3, sound buzzer
4, click 2 solenoids and light an LED
5, wait for second button
but there it fails, i want the 2nd button to trigger a relay to drive a motor, this is my code:-

#define step_pin 3
#define dir_pin 2
#define MS1 5
#define MS2 4

const byte button1 = 8;
const byte button2 = 9;
 int relay1 = 12;
 int dir;
 int steps = 510;
 int solenoide = 10;
 int led =13;
 int buzzer = 11;
 
void setup() {
   pinMode(relay1, OUTPUT);
   digitalWrite(relay1, HIGH);
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   digitalWrite(MS1, LOW);
   digitalWrite(MS2, LOW);
   digitalWrite(dir_pin, LOW);
   pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);
   pinMode(solenoide, OUTPUT);
   pinMode(led, OUTPUT);
}

void loop() {
  while (digitalRead(button1) == HIGH)
   {
   }
   delay(1000);
   
  int i=0;

 do{
  i++;
  tone(buzzer, 455);
  delay(320);
  noTone(buzzer);
  delay(500);
 }while(i<=0);
 
  digitalWrite(solenoide,HIGH);
  digitalWrite(led, HIGH);
  delay(250);
  digitalWrite(solenoide,LOW);
  digitalWrite(led, LOW);

  while (digitalRead(button2) == HIGH) 
  {
  }
  
  digitalWrite(relay1, LOW);
  delay(1000);
  digitalWrite(relay1, HIGH);
  delay(500);
  
  while(steps>=0) {       
  digitalWrite(step_pin, HIGH);
  delay(3);
  digitalWrite(step_pin, LOW);
  delay(3);
  steps--;
  
  }
}

i can get the relay to work if i set it up on another arduino like this:-

const byte button1 = 9;
int relay1 = 12;

void setup() {

  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, HIGH);
  pinMode(button1, INPUT_PULLUP);

}

void loop() {
  while (digitalRead(button1) == HIGH)
   {
   }  
  digitalWrite(relay1, LOW);
  delay(1000);
  digitalWrite(relay1, HIGH);
}

can anyone advise me on what im doing wrong,
thanks,
phil.

Sorry my mistake again, the switch wire wasn’t quite pushed in the breadboard enough, the code is good, moving onto the next part.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.