First Serial LED Project

$20 Paypal transfer if you help me with code, if interested please PM me.

Trying to write a code for a simple counter, just like a switch, if the circuit is closed +1 to the count, and so on. I’ve been using the code through the below link below, but since I’ve learned there is no driver display??? http://missionduke.com/wordpress/arduino-projects/ See the first project “Button Press Counter”.

There are two difference between the link above and my setup, he used a Mega, I have an Uno and our displays are a little different, I have a Tindie LED Display and he is using a sparkfun project. So not much difference, I think I am good, just need a working code.

So, if you can help me piece together a working code, I’ll finally have my first working project and something I could build on to learn more Arduino in the future.

As I mentioned above, I’d like to pay someone that could help me produce my first code and I’ll paypay you $20. I work sometimes 7 days a week, with a family at home and just don’t have the time right now to learn the proper code I need, so for this time I’d like to pay someone for their time.
Thxs again if you can help. Code attached below, but you can get all the details from the link above,
Cheers fellas!
NickerNerd

#include <SoftwareSerial.h>  //for software serial communication



#define txPin 1  //change to your serial port on Arduino board
#define rxPin 0  //not used but is required

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
int buttonPressCount;


const int  buttonPin = 2;    //the pin that the pushbutton is attached to




int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button


void setup()  {
  pinMode(buttonPin, INPUT);  //initialize the button pin as a input
  Serial.begin(9600);  //initialize serial communication

 pinMode(txPin, OUTPUT);
 //the following resets the board, changes the brightness to 100%, and sets the board to '0000':
 mySerial.begin(9600);
 mySerial.print((byte)0x7a); //special character
 mySerial.print((byte)0x00); //set brightness to full
 mySerial.print((byte)0x76); //reset board
 mySerial.print(0); //send '0' character
 mySerial.print(0); //send '0' character
 mySerial.print(0); //send '0' character
 mySerial.print(0); //send '0' character
}

void loop(){
 
  buttonState = digitalRead(buttonPin);  //read the pushbutton input pin

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
     
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      updateDisplay(buttonPushCounter);  //function to update the display 'requires button press count'
     
     
    }

  }
 
  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop

 

}




void updateDisplay(int buttonPushCounter){
String intString = String(buttonPushCounter);  //changes integer to a string
char displayChars[4];  //create array to hold the four numbers
int stringLength = intString.length();  //get length of the string
//the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
//and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
if(stringLength == 4){
  displayChars[0] = intString.charAt(0);
  displayChars[1] = intString.charAt(1);
  displayChars[2] = intString.charAt(2);
  displayChars[3] = intString.charAt(3);
}else if(stringLength == 3){
  displayChars[0] = 0;
  displayChars[1] = intString.charAt(0);
  displayChars[2] = intString.charAt(1);
  displayChars[3] = intString.charAt(2);
}else if(stringLength == 2){
  displayChars[0] = 0;
  displayChars[1] = 0;
  displayChars[2] = intString.charAt(0);
  displayChars[3] = intString.charAt(1);
}else if(stringLength == 1){
  displayChars[0] = 0;
  displayChars[1] = 0;
  displayChars[2] = 0;
  displayChars[3] = intString.charAt(0);
}
 mySerial.print((byte)0x76); //Reset board
 mySerial.print((byte)0x76); //Reset board
 mySerial.print(displayChars[0]); //Send '0' character
 mySerial.print(displayChars[1]); //Send '0' character
 mySerial.print(displayChars[2]); //Send '0' character
 mySerial.print(displayChars[3]); //Send '0' character

delay(100); //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'
 
}

Trying to write a code for a simple counter, just like a switch, if the circuit is closed +1 to the count, and so on.

If the circuit IS closed? Or, if the circuit BECOMES closed? Big difference. The state change detection example if BECOMES is appropriate (as it probably is).

#define txPin 1  //change to your serial port on Arduino board
#define rxPin 0  //not used but is required

Do NOT even consider doing SoftwareSerial on the hardware serial pins.

  Serial.begin(9600);  //initialize serial communication

Especially not when doing hardware serial on them. 8)

Not knowing which display you have does not help. But the ones I found use the MAX7219 chip with SPI and there is sample code.

See e.g. New version of MAX7219 based 4-digit serial seven segment LED display and you might be better of basing your code on those examples.

Hey PaulS, thxs for the reply. Ok, just a missed word in my explanation, but is the code correct in that part?

#define txPin 1  //change to your serial port on Arduino board
#define rxPin 0  //not used but is required

Ok, I will not use SoftwareSerial on the hardware serial pins. can you point in direction that helps me better the code? You obviously have a lot of experience, can you help me a bit further? Thxs.

but is the code correct in that part?

If the switch is wired correctly, yes.

can you point in direction that helps me better the code?

Your updateDisplay() function is awful. Converting an int to a String, and then extracting the string from it one character at a time, and then diddling with the possible leading 0s is way more difficult than

char displayChars[5];
sprintf(displayChars, "%04d", buttonPushCounter);

Thxs PaulS, yepper the switch is wired correctly, sorry again for not wording it property in my initial explanation.

PaulS you mentioned "Your updateDisplay() function is awful",.. this is not actually my code, just someone's code I am trying to learn from. Any chance you can lend me a little more guidance rather than tallying all the corrections that are needed? You are obviously very experienced, a newbie like me could benefit a lot from your guidance, this is what this section is called, "Project Guidance". Help me a bit to push me forward, tell me how I should correct my code, or suggest another place I could go for help.

Thxs PaulS

Hey sterretje, Thxs for link, but I think you missed my attachment, it is actually the same display board IO have lol.

I've tried the sample code, but sadly there is an error, I'm working on it ... Thxs again sterretje

NickerNerd: Hey PaulS, thxs for the reply. Ok, just a missed word in my explanation, but is the code correct in that part?

#define txPin 1  //change to your serial port on Arduino board
#define rxPin 0  //not used but is required

Ok, I will not use SoftwareSerial on the hardware serial pins. can you point in direction that helps me better the code? You obviously have a lot of experience, can you help me a bit further? Thxs.

Arduino Serial uses the 1st hardware serial port. A Mega has Serial1, Serial2 and Serial3 as well. Some breadboardable AVR's have 2 serial ports (Serial and Serial1), the 1284P has 16K ram as well.

Only use SoftwareSerial when you've run out of serial ports, it is CPU intensive.

Note that all AVR serial ports are capable of full speed master-mode SPI.

NickerNerd:
I’ve tried the sample code, but sadly there is an error, I’m working on it …
Thxs again sterretje

I’m sorry, at a second look it might not be real SPI for that board. Does not matter too much as I suppose the code should work.

It requires the LedControl library though. Do you have it? If not, you can try github/wayoda/ledcontrol.

If you have errors, please post the complete error message so we can help; use code tags as you did for your code.