Hello to all of you,
I am totally new to the Arduino and trying to play a little bit around with the setup discribed in this tutorial:
All the Code Examples are working and so I added a Reed-Switch with a pulldown and started to write some code by myself.
The Switch should mesure the rotationspeed and the LEDs should light up for the quarter of one cycle.
As long as I leave the serial.Print Command at its place, it kind of works, but when I comment it out, nothing works anymore.
I whould be happy, if someone can explain it. Here is the Code:
// Constants
// Pin connected to Reedswitch
const int buttonPin = 7;
// Pin connected to single LED
const int ledPin = 13;
//Pin connected to ST_CP of 74HC595
const int latchPin = 8;
//Pin connected to SH_CP of 74HC595
const int clockPin = 12;
////Pin connected to DS of 74HC595
const int dataPin = 11;
// Variables
int buttonState = 0;
// Time for one Cycle
unsigned long cycleTime;
// Time at the Switch
unsigned long timeAtSwitch = 0;
// Age of the cycle
unsigned long age = 0;
// Byte for Shiftregister
int numberToDisplay = 0;
boolean nextRound;
void setup() {
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// enable serial Port
Serial.begin(9600);
}
void loop() {
// take the latchPin low
digitalWrite(latchPin, LOW);
// shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
//take the latch pin high so the LEDs will light up
digitalWrite(latchPin, HIGH);
Serial.println(numberToDisplay);
numberToDisplay = 0;
// read the Reed Switch
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && nextRound == true) {
digitalWrite(ledPin, HIGH);
cycleTime = millis() - timeAtSwitch;
timeAtSwitch = millis();
// Serial.println(cycleTime);
nextRound = false;
}
if (buttonState == LOW) {
digitalWrite(ledPin, LOW);
nextRound = true;
}
// age of cycle
age = millis() - timeAtSwitch;
if (age < cycleTime/4) {
Serial.println(age);
// all LEDs on
numberToDisplay = 255;
}
}