digitalWrite() function not working

Hi everyone,

I'm using Sparkfun's code from the hookup guide (button-pad-hookup-guide). Now I'm trying to use the digitalWrite() function in the code but it is not working properly :

The red digitalWrite() function is not working properly : What I mean by this is that there is no voltage. The blue digitalWrite() function does work and I can measure a 5V output. I can't figure out why the red digitalWrite() function does not bring the A0 pin to 5V and was wondering if anyone here has the answer?

Thanks in advance!

/******************************************************************************
red-plus-buttons.ino
Byron Jacquot @ SparkFun Electronics
1/6/2015

Bas Gerritsen @ Canisius College
1/25/2018
*/ 
//config variables
#define NUM_LED_COLUMNS (4)
#define NUM_LED_ROWS (2)
#define NUM_BTN_COLUMNS (4)
#define NUM_BTN_ROWS (2)
#define NUM_COLORS (1)

#define MAX_DEBOUNCE (3)

// Global variables
static bool LED_buffer[NUM_LED_COLUMNS][NUM_LED_ROWS];

static const uint8_t btncolumnpins[NUM_BTN_COLUMNS] = {6, 7, 8, 9};
static const uint8_t btnrowpins[NUM_BTN_ROWS]       = {2, 3};
static const uint8_t ledcolumnpins[NUM_LED_COLUMNS] = {10, 11, 12, 13};
static const uint8_t colorpins[NUM_LED_ROWS]        = {4, 5};

static int8_t debounce_count[NUM_BTN_COLUMNS][NUM_BTN_ROWS];

static void setuppins()
{
  uint8_t i;

  // initialize
  // select lines
  // LED columns
  for (i = 0; i < NUM_LED_COLUMNS; i++)
  {
    pinMode(ledcolumnpins[i], OUTPUT);

    // with nothing selected by default
    digitalWrite(ledcolumnpins[i], HIGH);
  }

  // button columns
  for (i = 0; i < NUM_BTN_COLUMNS; i++)
  {
    pinMode(btncolumnpins[i], OUTPUT);

    // with nothing selected by default
    digitalWrite(btncolumnpins[i], HIGH);
  }

  // button row input lines
  for (i = 0; i < NUM_BTN_ROWS; i++)
  {
    pinMode(btnrowpins[i], INPUT_PULLUP);
  }

  // LED drive lines
  for (i = 0; i < NUM_LED_ROWS; i++)
  {
    pinMode(colorpins[i], OUTPUT);
    digitalWrite(colorpins[i], LOW);
  }

  // Initialize the debounce counter array
  for (uint8_t i = 0; i < NUM_BTN_COLUMNS; i++)
  {
    for (uint8_t j = 0; j < NUM_BTN_ROWS; j++)
    {
      debounce_count[i][j] = 0;
    }
  }
}

static void scan()
{
  static uint8_t current = 0;
  uint8_t val;
  uint8_t i, j;

  // Select current columns
  digitalWrite(btncolumnpins[current], LOW);
  digitalWrite(ledcolumnpins[current], LOW);

  // output LED row values
  for (i = 0; i < NUM_LED_ROWS; i++)
  {
    if (LED_buffer[current][i])
    {
      digitalWrite(colorpins[i], HIGH);
    }
  }
  // pause a moment
  delay(1);

  // Read the button inputs
  for ( j = 0; j < NUM_BTN_ROWS; j++)
  {
    val = digitalRead(btnrowpins[j]);

    if (val == LOW)
    {
      // active low: val is low when btn is pressed
      if ( debounce_count[current][j] < MAX_DEBOUNCE)
      {
        debounce_count[current][j]++;
        if ( debounce_count[current][j] == MAX_DEBOUNCE )
        {
          Serial.print("Key Down ");
          Serial.println((current * NUM_BTN_ROWS) + j);

          for(int k =0; k < NUM_LED_COLUMNS ;k++){
            for(int q =0; q < NUM_LED_ROWS ;q++){
                LED_buffer[k][q] = 0;
              }
              
            }
          
          
          
          // Do whatever you want to with the button press here:
          [color=red]digitalWrite(A1, HIGH);[/color]
          // toggle the current LED state
          LED_buffer[current][j] = !LED_buffer[current][j];
          
        }
      }
    }
    else
    {
      // otherwise, button is released
      if ( debounce_count[current][j] > 0)
      {
        debounce_count[current][j]--;
        if ( debounce_count[current][j] == 0 )
        {
          Serial.print("Key Up ");
          Serial.println((current * NUM_BTN_ROWS) + j);

          // If you want to do something when a key is released, do it here:

        }
      }
    }
  }// for j = 0 to 3;

  delay(1);

  digitalWrite(btncolumnpins[current], HIGH);
  digitalWrite(ledcolumnpins[current], HIGH);

  for (i = 0; i < NUM_LED_ROWS; i++)
  {
    digitalWrite(colorpins[i], LOW);
  }

  current++;
  if (current >= NUM_LED_COLUMNS)
  {
    current = 0;
  }

}

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);

  Serial.print("Starting Setup...");

  // setup hardware
  setuppins();
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);
  pinMode(A6, OUTPUT);
  pinMode(A7, OUTPUT);
  [color=blue]digitalWrite(A0, HIGH);[/color]

  // init global variables
  for (uint8_t i = 0; i < NUM_LED_COLUMNS; i++)
  {
    for (uint8_t j = 0; j < NUM_LED_ROWS; j++)
    {
      LED_buffer[i][j] = 0;
    }
  }
  LED_buffer[0][0] = true;
  Serial.println("Setup Complete.");

}

void loop() {
  // put your main code here, to run repeatedly:

  scan();

}

A7?
What are you running this on?

Please use code tags - the forum has mangled your code and now it is incomprehensible.

Please go read the forum guidelines in the sticky post. Your question makes no sense and you posted your code and link incorrectly. Please edit and fix your post above.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What model Arduino are you using?
What is your code supposed to do?

Can you please tell us your electronics, programming, Arduino, hardware experience?

Have you written a simple test code to check that the output works?

Tom... :slight_smile:

I'm running this on a Arduino Pro Mini with ATmega168 on 5V 16MHz. The code detects when and which button is pressed, now I want the code to put an analog pin on low or high depending on which button is pressed.(I'm in the process of coming up with the code for this.) But I can't seem to figure out why the red digitalWrite() function doesn't set the A0 pin on 5V.

And I have some programming experience in java and c++ but nothing too spectacular. And just started using the Arduino. No further hardware experience

A7 is an analogue input-only pin - you can't set it to be a digital input or output.
(same for A6)

Wow, didn't realise that. Thanks for telling!

I can't seem to figure out why the red digitalWrite() function doesn't set the A0 pin on 5V.

Maybe because the digitalWrite() line that you marked red
digitalWrite(A1, HIGH);
refers to pin A1

Thanks for reading the forum guidelines and fixing your link and code tags.

No problem PaulRB, should've done it in the first place.

And UKHeliBob I can't believe it but that was the problem.., I just got the measurements and ports mixed up in my head..

Thanks for helping everyone!