0

I'm very new to coding so please don't grill me.

I'm working on a scientific project where I am using a NI-DAQ to digitally trigger mosfets to trigger 300us - 3ms pulses on two high powered LED arrays. My intention is to use the Arduino UNO to basically relay one digital output into many. It remains important to use the NI-DAQ as the pulses are dependent on video cues and other outputs, otherwise the pulses would be purely produced by the Arduino.

I have decided to the port Port reg output to accomplish this as it would appear to have the greatest temporal precision but I’m having a very difficult time getting a single pin to trigger a port. I would greatly appreciate some help.

My code currently looks as such:

int inpin = 2; //input specified as pin 2
int x = 0; // variable to store the read value


void setup() {
pinMode(inpin, INPUT); //sets digital pin 13 as input
DDRD=B11111111; //pin 8-13 is in output mode
DDRC=B11111111; //pin A0-5 designated output
}

void loop() {
x = digitalRead(inpin); //read the input pin
if (inpin = HIGH) PORTB=B11111111; // if digital input is high, all Bport high
if (inpin = HIGH) PORTC=B11111111; // if digital input is high, all Cport is high
if (inpin != HIGH) PORTB=B00000000; // if digital input is low, all Bport is low
if (inpin != HIGH) PORTC=B00000000; // if digital input is low, all Cport is low

}

At the moment it seems the board it reading the input but is not relaying the outputs correctly with the PORTB continuously off and PORTC continuously on. Any help would be great!

  • 2
    If your purpose is to just repeat an input why not take an IC meant for the task, They are commonly reffered to as buffers. – ratchet freak Mar 28 '19 at 13:19
  • Check the Arduino pinout. It would seem you mixed up ports B and D. – Edgar Bonet Mar 28 '19 at 13:19
  • Thank you Edgar, I fixed my typo in the set up area. Now both B & C are illuminated continuously. – JTS Mar 28 '19 at 13:45
  • I agree with ratchet. If you just want to repeat a digital input then use a buffer ship. Those respond in single-digit nanoseconds, with no programming needed. Arduino chips are relatively slow. – Duncan C Mar 28 '19 at 15:21
2

You have some coding errors. You likely meant to write something like:

void loop() {
  x = digitalRead(inpin); //read the input pin
  if (x == HIGH) PORTB=B11111111; // if digital input is high, all Bport high
  if (x == HIGH) PORTC=B11111111; // if digital input is high, all Cport is high
  if (x != HIGH) PORTB=B00000000; // if digital input is low, all Bport is low
  if (x != HIGH) PORTC=B00000000; // if digital input is low, all Cport is low
}

Which could be simplified to:

void loop() {
if (digitalRead(inpin) == HIGH)
{
  PORTB=B11111111; // if digital input is high, all Bport high
  PORTC=B11111111; // if digital input is high, all Cport is high
}
else
{
  PORTB=B00000000; // if digital input is low, all Bport is low
  PORTC=B00000000; // if digital input is low, all Cport is low
}
}
  • Amazing! Thank you, I had been stuggling with that for some time. I thought an else statement would be apropriate I just didn't know how! Cheers – JTS Mar 28 '19 at 15:58
0

If you are doing this sort of low-level port manipulation, I suggest you ditch the Arduino core and go the avr-libc way:

#include <avr/io.h>

int main(void)
{
    DDRB = 0xff;  // all port B as output
    DDRC = 0xff;  // all port C as output
    DDRD = 0;     // all port D as input
    for (;;) {
        if (bit_is_set(PIND, PD2)) {  // if PD2 reads HIGH
            PORTB = 0xff;  // all port B goes HIGH
            PORTC = 0xff;  // same for port C
        } else {
            PORTB = 0;     // all port B gos LOW
            PORTC = 0;     // same for port C
        }
    }
}

This compiles to very efficient code that takes at most 7 CPU cycles per loop iteration. In contrast, a call to digitalRead() can take hundreds of cycles, greatly increasing the program latency.

Obviously a buffer would still be way better in terms of latency, as pointed out by ratchet freak in a comment.

  • I'll explore this option too. Although latency is important In my project it is most important that each pin within a port in in sync with the rest of the pins within the same port. I believe this approach, similar to the buffer approach, will be sufficient. Thank you – JTS Mar 28 '19 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.