Newbie here,
I have a program coded properly so that when I push a button my my breadboard, 2 leds on it light up, and the time which they were pushed at (that is, the time elapsed since the serial monitor was opened) is displayed on the serial monitor. These times are then recorded into an array, and at the end of the program - after 2 pushes of the button - all recorded results written to the serial monitor.
When the button is pushed, it should send a "HIGH" to the function 'void all_write(int *buttonState)' to tell the LED pins to turn on. If the button is not pushed, the LEDs are off.
I can get the program working using functions of pass by value, but if I try to modify the program to use pass by reference, particularly using "buttonState", I get errors, beginning on line 90: "invalid conversion from 'int' to 'int*'.
I can use pointers in c++, but cannot seem to get it to work in Arduino. Any ideas how to modify my code to make this work how I'm trying to? Just trying to do something basic / simple, that's all.
/*****
* This program will have 2 LEDs when the button is pressed.
*
* Formating
* 1. The timer() function is only used for the serial monitor to display current time
* 2. The data is written to an array. Currently, it is a 2x3 array, with these characteristics:
*
* Columns:
* 1: # at which instance button has occured (e.g. 1,2,3...10)
* 2: minutes
* 3: seconds
*
* When array is eventually printed, it will be in this format:
*
* 1 0 34
* 2 1 25
*
* You have to know on the program you input it to which column is which, as it is being
* exported as type int variables.
*****/
#include <Time.h>
// constants
const int buttonPin = 7; // push button
const int ledPin = 13; // red
const int ledPin2 = 2; // green
// variables
int *buttonState = LOW; // variable for reading the pushbutton status
int i = 0, j = 0;// start at cell 0x0 in array
int instances = 1; // number of times button was pushed
// array for putting instances & time-stamp (min, sec) into, 2x3 array: instances, time
const int rows = 2;
const int columns = 3;
int my_array[rows][columns];
// booleans
bool decision = true; // end of program
//inputing button state to output of LEDs states
void all_write(int *buttonState)
{
digitalWrite(ledPin, *buttonState);// = buttonState; // HIGH or LOW, depending what is fed to it
digitalWrite(ledPin2, *buttonState);// = buttonState; // HIGH or LOW, depending what is fed to it
}
// timer in format: MM:SS
void timer(void)
{
// minutes
if (minute() < 10)
{
Serial.print("0");
Serial.print(minute());
}
else
{
Serial.println(minute());
}
Serial.print(":");
// seconds
if (second() < 10)
{
Serial.print("0");
Serial.println(second());
}
else
{
Serial.println(second());
}
}
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT); // sets pin 7 as an input
pinMode(ledPin, OUTPUT); // sets pin 13 as an output
pinMode(ledPin2, OUTPUT); // sets pin 12 as an output
}
void loop()
{
while (i < rows) // condition to check for program to run
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin); // is this needed?
if (digitalRead(buttonPin) == HIGH)
{
all_write(&buttonState); // both ledPIN and ledPin2 set to HIGH
// display data, instance & time button push occured at
Serial.print(instances);
Serial.print(": ");
timer();
// place data into array, cell by cell
my_array[i][j] = instances;
j++; // move to the right by 1 column
my_array[i][j] = minute();
j++; // move to the right by 1 column
my_array[i][j] = second();
// go to next row of column
i++; // move down 1 column
j = 0; // move back to the left most column
instances++;
}
else
{
all_write(&buttonState); // both ledPin and ledPin2 set to LOW
}
delay(250); // how often loop will run
}
//end of program
if (decision == true) // run only once
{
Serial.println("Program is over!");
decision = false;
all_write(LOW); // both ledPin and ledPin2 set to LOW
//print table - use table from c++ program as model using the function command?
//print_table(my_array, rows, columns);
Serial.println();
Serial.println("Show full table:"); //cout << "Show full table: " << "\n" << endl;
Serial.println("(Note: Raw data is being exported a 2x3 array, with the columns headers as 'Instances', 'Minutes', 'Seconds')");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Serial.print(my_array[i][j]);
Serial.print(" ");
}
Serial.println("");
}
Serial.print("");
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.