I am having difficulty getting the sketch below to work. The aim was to develop a binary counter by utilising a while loop with a bitwise conditional operator, which uses a function which increments or decrements the count.
I also need to be able to start and stop the counter by entering A or B on the keyboard.
The issue i'm having is it doesn't seem to be counting upwards it should reach 63 before counting back down and also function does not stop when i enter B on my keyboard.
Any help/advice would be greatly appreciated by this newbie
// include the library code:
#include <LiquidCrystal.h>
// Sets up Global variales for serial data
bool stringComplete = false;
// Sets character input to serial monitor
char choice;
// Sets up out array of data
//byte values[63] ;
byte startByte = B0000001;
volatile bool on = false;
// initializes the library and defines the LCD interface pin numbers
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
// Defines the ports
DDRB = B11000000; //defines the port as read only
DDRC = B00111111; //defines the port as write only
// Defines which of the LCD's columns and rows will be used
lcd.begin(16,2);
// Message to greet the user
lcd.print("Kenny's Counter");
Serial.begin(19200);
// creates 1 second delay
delay(1000);
}
void loop()
{
// Set the cursor to column 0, line 1
lcd.setCursor(0, 1);
// check if the data has been received from the serial port
byte receive;
// start of the if statement
if (on)
{
// defines the count up loop
while((startByte <= B00111111 || startByte >= B00000001) && true)
{
// debug print
Serial.println(startByte);
// sends the value to the port C
PORTC = startByte;
// peads the value to the port B
receive = PINB;
// sets the cursor
lcd.setCursor(0, 0);
// clear the LCD
lcd.clear();
// print the number received to lcd
// the BIN defines a binary output is wanted
lcd.print(receive, BIN);
delay(300);
Serial.println(startByte);
}
void countUp();
{
if(!countUp)
{
startByte--;
}
else(countUp);
{
startByte++;
}
}
}
}
void serialEvent(){
while (Serial.available()){
// read the new received byte
char inchar = (char) Serial.read();
// set the input string to the input char
choice = inchar;
// set the flag so that the loop will know to display the received data
stringComplete=true;
Serial.println(choice);
if(choice == 'A')
{
on=true;
}
if (choice =='B')
{
on = false;
}
Serial.println(on);
}
}