# Rookie Help! First time with Arduino

Hi guys, im having trouble understand how Arduino reads digital pins. I got given sample code that read the binary input of one of the digital pins and got told to make it read two and then write the results onto EEPROM in a 30x32 matrix. What i do not understand is why in the sample code, there is no mention of the digital pin 4, which is the pin that the input is connected to.

Basically the flash function is the mcTimer2 set at 500 ms and start state is the 9th pin connected via a resistor the 8th ping (which is set at output HIGH)

void flash() {
// read the state of the pushbutton value:

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (StartState == HIGH)
{
if(ee_clear)
{
ee_clear = false;
for (int i = 0; i < 960; i++)
{
EEPROM.write(i, 0);
}
}

digitalWrite(led, output);
output = !output;

//Serial.println(TCNT0);
{
}
TCNT0 = 0;
}
}

Also TCNT0 is declared at the beginning to be 0x00, can someone pleaseee explain to me what TCNT0 is and what the hex number symbolises??

Much appreciated! I am very new to coding

My guess (though it is only a guess, since you didn't post your entire code) is that start2 is defined as 4 at some point in the code.

TCNT0 is the counter register for timer 0. This is the value that increments with each "tick" of the timer.

Hex is a base 16 numbering system. Instead of 0 to 9 it uses 0 to 9 and A to F.

0x00 = 0
0x01 = 1
0x02 = 2
0x03 = 3
...
0x09 = 9
0x0A = 10
0x0B = 11
...
0x0F = 15
0x10 = 16
0x11 = 17
... etc ...

Ohh so thats what TCNT0 is, oh and thats the problem, this is the whole code

#include <MsTimer2.h>
#include <EEPROM.h>

boolean s_received = false; // whether the string is complete
char inChar;
static boolean output = HIGH;
int StartState = 0; // variable for reading the Start status
boolean ee_clear = true; // to clear eeprom

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
const int led = 13; // the number of the LED pin
const int start1 = 8; // the number of the starter1 pin
const int start2 = 9; // the number of the starter2 pin

void setup()
{

//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.println("To start EEPROM data transmission, please send an 's'.");

// Timer/Counter 0 initialization
// Clock source: T0 pin Rising Edge
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x07;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(start1, OUTPUT);
digitalWrite(start1, HIGH);
// initialize the pushbutton pin as an input:
pinMode(start2, INPUT);

MsTimer2::set(500, flash); // 500ms period
MsTimer2::start();
}

void loop() {
}

void serialEvent() {
while (Serial.available())
{
// get the new byte:
if (inChar == 's')
{ // print out logged data
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 29; j++)
{
Serial.print(" ");
}
30+29));
}
}
}
}

void flash() {
// read the state of the pushbutton value:

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (StartState == HIGH)
{
if(ee_clear)
{
ee_clear = false;
for (int i = 0; i < 960; i++)
{
EEPROM.write(i, 0);
}
}

digitalWrite(led, output);
output = !output;

//Serial.println(TCNT0);
{
}
TCNT0 = 0;
}
}

I tested the Arduino with the sample code and one sensor connected to pin 4 and it worked, recorded the data and everything, I just have no idea how it worked because theres no relation to the 4th pin, and without that knowledge i cant go on and attach 2 sensors to it

Sash95:
// Timer/Counter 0 initialization
// Clock source: T0 pin Rising Edge
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x07;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

Sash95:
I tested the Arduino with the sample code and one sensor connected to pin 4 and it worked, recorded the data and everything, I just have no idea how it worked because theres no relation to the 4th pin, and without that knowledge i cant go on and attach 2 sensors to it

Yeah, it's far from obvious how pin 4 is involved, but I think the clues are here:

``````EEPROM.write(ee_addr, TCNT0);
``````

The value you're writing is the Timer0 control register, and you've configured Timer0 to be triggered from the rising edge of a clock source, and Timer0 is associated with pins 4 and 13. I'm not familiar with mucking around with the hardware timers directly like this, but I suspect what's happening is that your external input on pin 4 is triggering Timer0 and this is resulting in the values you're reading from the Timer0 control register later on - the code is not explicitly reading from pin4 anywhere.

I'd regard this code as devious and complicated and not reasonable to ask a novice programmer to make substantial changes to.

In that case would it be okay to change the code around and set pin 4 as an input? Although I'm not sure if the code would still register the "amount" of trips made by an external photo interuptor per 500 ms byte

Sash95:
In that case would it be okay to change the code around and set pin 4 as an input? Although I'm not sure if the code would still register the "amount" of trips made by an external photo interuptor per 500 ms byte

I'm only guessing what the code is supposed to do, but I think it's relying on pin 4 to trigger the timer. In that case, any changes to the use of pin 4 could break it. Before you make any substantial changes I suggest you need to be clear what the existing code is supposed to achieve and how it achieves it, and what you want your new code to achieve. So far none of that is clear to me, and I wouldn't want to touch that code with a barge pole.

what i meant was can I remove TCNT0 all together from the code, and just use mctimer2 to record the data, while setting pin 4 as an input