dual screen tapper programming/code assistance?

Hi!!

I need some code assistance, please. I cannot figure out how to get pin 13 or 11 tap at a fast speed and the other pin tap much slower. This needs to be in a continuous loop, no delays. Is it possible? Here is what I have now, but I am tired of carrying 2 Arduinos. Can't one Arduino do what I want? Please assist me. I searched Google as much as I could. I gave up! Thank you. I really appreciate it.

int touch1 = 13;
int touch2 = 11;

void setup() {
pinMode(touch1, OUTPUT);
digitalWrite(touch1, LOW);

pinMode(touch2, OUTPUT);
digitalWrite(touch2, LOW);
}

void tap(int pin)
{
digitalWrite(pin, HIGH);
delay(63);
digitalWrite(pin, LOW);

}

void loop() {

tap(touch1);

tap(touch2);

}

I cannot figure out how to get pin 13 or 11 tap at a fast speed and the other pin tap much slower.

Use millis() for timing s. See
Using millis() for timing. A beginners guide so that the program is not blocked at any time.

It would help if you gave a clearer explanation of what you want to do

It appears that you are attempting to generate a 63mS pulse (approx 50% duty cycle) on pin 13, and the same on pin 11 (but with inverted phase) in a repeating cycle (about 8 Hz).

You talk about 2 Arduinos. How are these interconnected together with your application and for what purpose ?

Hi guys! Thank you for replying. Yes I am tapping my iphone screen, playing tapping games. I carry 2 Arduinos with the same code above programmed to both. However, one Arduino/code is slightly modified to allow for the 16ms speed. I use a lithium ion battery pack with 2 USB plugs to power the Arduinos. I only plug 1 tapper in to pin 13 on one Arduino and the other Arduino 1 tapper is plugged in to pin 11.

I basically need pin 13 to run at a continuous 63ms and pin 11 to run at a continuous 16ms with no delays on only 1 Arduino.

Hope that was a clearer explanation and makes sense.

Thank you!

Set a timer with 1mS ticks.
Have 2 counters in the timer iSR.
Set/Reset the counters and pins as required to give the desired pulse widths.

Any chance you can show me what it would look like? Please. I am rather beginner level here. Haha

Thank you

Please, someone help me out? I’d really appreciate it.

How long should each tap last ?

Please, someone help me out? I'd really appreciate it.

Stary by looking at the BlinkWithoutDelay example. It will show you how to do a symmetrical very slow "tap" to one pin.

Change from millis() timing to micros() and you can have faster "taps".

Change the period at each change of output state and you can have a faster asymmetrical "tap".

Repeat the code in loop() using different parameters and variable and you can have 2 fast, asymmetrical "taps"

UKHeliBob, I really appreciate your attempt to help me out. But, you’re speaking greek to me. I should have mentioned that I did not write the original code above and have never personally wrote any code for Arduino or anything. Unfortunately, I cannot get a hold of the person who wrote the code above. I been attempting to research the code and possibly just rewrite it, too bad I just can’t figure it out.

I want the pins to tap at a speed I can adjust accordingly, start out with pin 13 at 62ms and pin 11 at 16ms. I just need the pins to tap continuously, no delay between taps like a tap tap tap tap...etc.

Oh yeah, to answer your question, no end to the tapping. Tap continuously.

Thank you

to answer your question, no end to the tapping. Tap continuously.

That is not what I meant. What I had in mind was how long does each tap last

As to the program, try the BlinkWithoutDelay example. Does the Arduino built in LED flash on and off ?

You can also do it like this as described in post #4. You still have to answer the question about the spaces between the pulses. I've assumed a 50% duty cycle below.

#include <TimerOne.h>   // you may have to install this

int touch1 = 13;
int touch2 = 11;

unsigned long touch1_MARK_ms = 63 ;  // tune these parameters to suit your application
unsigned long touch1_SPACE_ms = 63 ;
unsigned long touch2_MARK_ms = 16 ;
unsigned long touch2_SPACE_ms = 16 ;

void Timer_ISR() {
   // called every 1ms
   static unsigned int touch1_counter = 0 ;
   static unsigned int touch2_counter = 0 ;

   // touch1
   if( touch1_counter == 0 )  digitalWrite(touch1, HIGH );
   else if ( touch1_counter == touch1_MARK_ms )  digitalWrite(touch1, LOW );
   touch1_counter ++ ;
   if ( touch1_counter == touch1_MARK_ms + touch1_SPACE_ms ) touch1_counter = 0 ;

   // touch2
   if( touch2_counter == 0 )  digitalWrite(touch2, HIGH );
   else if ( touch2_counter == touch2_MARK_ms )  digitalWrite(touch2, LOW );
   touch2_counter ++ ;
   if ( touch2_counter == touch2_MARK_ms + touch2_SPACE_ms ) touch2_counter = 0 ;
  
}


void setup() {
  pinMode(touch1, OUTPUT);
  pinMode(touch2, OUTPUT);

  Timer1.initialize(1000);         // initialize timer1 1000 uS period
  Timer1.attachInterrupt(Timer_ISR);  // attaches Timer_ISR() as a timer overflow interrupt
}


void loop() { }

Hi guys, I am not sure how long each tap lasts. I think it just follows the default blink code loaded to the Arduino board. The current tap speed between taps is perfect. Yes, the LED on the Arduino blinks in sequence with my tappers.

The basic Blink sketch that the Arduino comes loaded with and which is included in the examples with the IDE turns on the LED for one second then turns it off for one second. To help you we need to know how long each output should remain turned on each time it happens.

If you are happy with equal on and off periods for each tapper, albeit at different frequencies, that is easier to program.

Untested beyond compiling OK

unsigned long periods[] = {63, 16};
unsigned long startMillis[2];
unsigned long currentMillis;
const byte outputPins[] = {13, 11};
byte currentPin;

void setup()
{
  Serial.begin(115200);
  for (int pinNum = 0; pinNum < sizeof(outputPins); pinNum++)
  {
    pinMode(outputPins[pinNum], OUTPUT);
  }
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - startMillis[currentPin] >= periods[currentPin])
  {
    digitalWrite(outputPins[currentPin], !digitalRead(outputPins[currentPin]));
    startMillis[currentPin] = currentMillis;
  }
  currentPin %= 2;
}

Ok, It has been awhile but looking back at my instructions. I remember I loaded the basic blink pattern

Thank you for both code examples! I really appreciate it guys!!

UKHeliBob,

Would I paste your code you provided as a new code? Or do I paste it somewhere in my original code in my first post? Sorry, I apologize. I am incredibly dumb when it comes to this coding stuff to be honest.

Yes, I am happy with equal on/off taps.

Thank you

Would I paste your code you provided as a new code?

The code that I posted is a complete program. Do a File/New in the IDE. Delete all of the text in the default template that should appear and paste in the whole of my program. Compile and upload it and report back what happens and how/if it does what you want.

Just spotted a bug in my program
Add

 currentPin++;

on the line before

  currentPin %= 2;

so now it reads

 currentPin++;
  currentPin %= 2;

Wow!!! You are the man UKHeliBob!!!!

Not to discredit 6V6GT!! I really appreciate both of you.

At first UKHeliBob, pin 11 wasn’t tapping at all. I was going to write back and tell you, then I noticed your response about a bug you found. I added that line and now it works PERFECTLY!!!

I am sooo happy! Thanks again

I am glad that it works. The 6V6GT version is a different way of doing the same thing. Did you try it ?

Now a challenge for you.
Add comments to my program describing how it works.

Also, have you ever explained exactly what devices you are using to actually produce the taps ?