newbie need some help!

Hello! I am trying to write a script to when i press a arduino button my say "z" key will start being pressed at random intervals. Then when I press the ardunio button again this action will stop completely. I am having trouble getting the "z" key to fire at random intervals. I am using the delay(random(x,x)) function but it doesn't seem to be doing anything. Unfortunately I am very new to all this and any help would be appreciated. Thank you

Try to imagine we don’t know what your thoughts are (like, what a ‘z key’ is)

Now - try to explain your problem.

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

might be easier if you post your code

sounds like you want some random processing to start when you press a button and stop when you press the button again.

it's not obvious how long your random events can be. if you use delay(), you won't be able to check for a button press until the delay returns and you will need to hold the button pressed until delay returns.

koldmana:
I am using the delay(random(x,x)) function but it doesn't seem to be doing anything.

Remember that delay() is working in thousandths of a second. What values do you use for 'x' and 'x'?

im just trying to get it to press the “z” key on my keyboard once say every 30secs to 1min. So far i can just get it to press the “z” repeating over and over with no random delay that i can see

#include <Keyboard.h>
 
void setup() {
  pinMode(2,INPUT_PULLUP); 
randomSeed(analogRead(A0));
 Serial.begin(9600);    //begin seral comms for debugging
}

void loop() {
  Keyboard.begin();
  if (digitalRead(2) == 1) //if button 3 is pushed
   {
Keyboard.press(0x20);
delay(random(10000, 150000));
}
}
 pinMode(2,INPUT_PULLUP);

If the switch is wired properly (to ground and an input) the input will read HIGH (1) when the switch is open (not pressed) and LOW (0) when closed (pressed).

koldmana:
im just trying to get it to press the “z” key on my keyboard once say every 30secs to 1min. So far i can just get it to press the “z” repeating over and over with no random delay that i can see

  • What makes you think that 0x20 is the ASCII code for ‘z’? 0x20 is the Space character.
  • If you want a 30 to 60 second delay, why use 10 to 150 seconds?
  • Is Pin 2 open (HIGH) or grounded (LOW) when you press the button? How many minutes did you press the button?
#include <Keyboard.h>

void setup()
{
  Serial.begin(9600);    //begin seral comms for debugging
  pinMode(2, INPUT_PULLUP);
  randomSeed(analogRead(A0));
  Keyboard.begin();
}

void loop()
{
  if (digitalRead(2) == LOW)
  {
    Keyboard.press('z');
    delay(random(30000, 60000));
  }
}