//Next things to do
//Work through pins - Address pins next!!
int clockS= LOW;
//pin assignments
//Pin fOr the clock generation (would normally be a crystal oscillator, so this simulates an oscillator)
const int clockPin = 23;
//Test pin for showing an output when testing
const int testPin = 53;
//the switch connect to pin 12
const int switchPin = 25;
const int buttonPin = 27; // the number of the pushbutton pin
//Clock speed in hz
const float clockSpeed = 1;
const float interval = 1 / clockSpeed * 500;
//initialise the var to store previous time in the clock function
unsigned long long previousMillis = 0;
//initialise the var to store current time in the clock function
unsigned long long currentMillis = 0;
//initialise the var state of the clock as low
int clockState = LOW;
int switchState = LOW; // variable for reading the pushbutton status
int buttonState = LOW; // variable for reading the pushbutton status
//Variable function for the clock
int clock(long clockSpeed){
//store the current number of mS since starting the arduino in a variable
currentMillis = millis();
//check if half interval has passed, if it has then change the clockState (half interval as clock cycle is HIGH to HIGH)
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (clockState == LOW) {
clockState = HIGH;
digitalWrite(clockPin, HIGH);
} else {
clockState = LOW;
digitalWrite(clockPin, LOW);
}
return clockState;
}
}
void setup() {
//set pinMode for each pin
pinMode(clockPin, OUTPUT);
pinMode(testPin, OUTPUT);
pinMode(switchPin, INPUT); //initialize thebuttonPin as input
pinMode(buttonPin, INPUT);
}
void loop() {
//run the clock
switchState = digitalRead(switchPin);
buttonState = digitalRead(buttonPin);
if (switchState == HIGH ) //if it is,the state is HIGH
{
int clockState = clock(clockSpeed);
digitalWrite(testPin, HIGH); //turn the led on
}
else
{
digitalWrite(testPin, LOW); //turn the ledoff
if (buttonState == HIGH)
{
if (clockState == LOW)
{
clockState = HIGH;
digitalWrite(clockPin, HIGH);
}
else
{
clockState = LOW;
digitalWrite(clockPin, LOW);
}
}
}
}