-4

I have an Arduino MEGA 2560 using <stdio.h> library. My aim is to connect a wire across an assigned output pin and the assigned input pins using a pull up resistor. I then want to print on the serial monitor which pins are connected. I cannot see anything on my serial monitor with the code below. Can someone please show me what I am doing wrong?

#include <stdio.h>

int main();

void setup() 
{
  // Use a Jumper wire to connect each input to each output

  DDRE |= (1 << DDE5);  // pin 3 (PE5) set as output
  DDRG |= (1 << DDG5);  // pin 4 (PG5) set as output
  PORTF |= (1 << PORTF0), (1 << PORTF1);  // Enable internal resistor on PF0 and PF1 input
}

void loop(void) 
{
  //Solar 1 --- Load xx
  PORTE = PORTE & B00100000;       // Turn on PE5 an output pin3:  Solar1
  _delay_ms(100);                  // Delay

  if (~PINF & B00000001);          // A0 input:  Load1
  {
    PORTB = PORTB & B01111111;     // Turn off Pin 13_LED
    printf("Solar 1 --- Load 1\n\r");
  }

  //Solar 2 --- Load xx
  PORTG = PORTG & B00100000;   //Make PG5 an output pin4:  Solar1
  _delay_ms(100);              //Delay

  if (~PINF & B00000010);      //A1 input:  Load2
  {
    printf("Solar 2 --- Load 1\n\r");
  }
}
Darryl Noakes
  • 2,207
  • 1
  • 9
  • 27
Handsome
  • 1
  • 2
  • 1
    What do you think `(1 << PORTF0), (1 << PORTF1)` does? – chux - Reinstate Monica Aug 26 '22 at 04:00
  • It turns on the internal resistor. – Handsome Aug 26 '22 at 04:06
  • 1
    Hmm, In that case, I thought you might want `(1 << PORTF0) | (1 << PORTF1)`? `|` (or) rather than sequential evaluation `,` – chux - Reinstate Monica Aug 26 '22 at 04:08
  • Arduinos don't just work like that, I'm afraid. `printf` usually prints to stdout, which does not exist on an Arduino. To send data over the serial port, you need to configure it using certain registers and then send data by writing to more registers and reading yet more registers to know when the data has been sent. It's complex... :) – Darryl Noakes Aug 26 '22 at 04:24
  • Just tried it and nothing happened. – Handsome Aug 26 '22 at 04:24
  • You could use the Arduino standard library, or try copying its code for the serial port. – Darryl Noakes Aug 26 '22 at 04:25
  • Other practicals I've done have worked fine using printf. – Handsome Aug 26 '22 at 04:26
  • as @DarrylNoakes said above `printf` would not work with arduino serial as it has no link. here are some examples from [Arduino Examples](https://docs.arduino.cc/built-in-examples/) for your understanding. – thirdeye Aug 26 '22 at 04:31
  • 3
    Is that your actual code? You seem to be using the Arduino library's `setup` and `loop` functions, but you have also defined your own `main` (which the Arduino library uses to call `setup` and `loop`); overriding the predefined `main` means that `setup` and `loop` are never called. – Darryl Noakes Aug 26 '22 at 04:32
  • Afraid I'm gonna necro this question... Did you ever reach a solution? Also, I just looked properly at the code you provided, and I don't think it even makes sense from a C++ perspective. You've got your if statements in the form `if (condition); {}`; that semicolon means the `if` does nothing and the block always runs (https://replit.com/@DarrylNoakes/Semicolon-after-if). – Darryl Noakes Apr 07 '23 at 22:02

1 Answers1

1

By using this include it doesn't mean it'll magically start working. On the computer you'll get everything prepared, but in MCU you'll have to setup stdout stream. And in the C++ you can't use FDEV_SETUP_STREAM and you'll have to do it directly.

#include <stdio.h>

FILE mystdout_serial;

int uart_putchar(char c, FILE *) {
    Serial.write(c); // or Serial.print(c);
    return 0;
}

void initStdout() {
    stdout = &mystdout_serial;
    mystdout_serial.buf = nullptr;
    mystdout_serial.flags = _FDEV_SETUP_WRITE;
    mystdout_serial.put = &uart_putchar;
    mystdout_serial.udata = nullptr;
}

void setup() {
    Serial.begin(115200);
    initStdout();

    printf("is it working?\n");
}

void loop() {}

Something like this should work..

KIIV
  • 3,534
  • 2
  • 18
  • 23