Hi there
I'm new here, I didnt want to come asking questions and intended to look about and try to figure it out.
But i cant find my issue :(
I havent been useing C for very long and am in no way "good" at it, but i know the basics.
What i was trying to d
Create a Reaction time test
Steps:
1. use a potinmeter (dial thing) to select from 3-9 and display on a 7 Seg display
2. Confirm number selected by pressing button1
3. Beep Buzzer to confirm selection
4. Run the number of tests according to selected number
Problem i havem is that It selects the number correctly and confirms it (without buzzer at this moment in time)
It then counts down to a LED light and i have to press button 2 to stop it.
BUT Its all looping without stopping, Buttons dont help and i have no idea where i have gone wrong.
If its entirely screwed up please say so i can just start again from scratch.
here is my code
[code]#define BUTTON1 10
#define BUTTON2 11
#define POT1 0
#define BUZZER 3
#define LATCH 7
#define CLOCK 8
#define DATA 4
#define LED1 6
const byte numbers[10] = {B10111101, B10101101, B10000101, B10110000, B10011001, B10010010, B10000010, B11111000, B10000000, B10010000 };
int DispNum = 0;
int potVar = 0;
int i = 0;
int times[9]; // Reaction time array
int count;
int start;
int j = 0; // Value for the reaction Time.
void setup()
{
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA,OUTPUT);
pinMode(LED1, OUTPUT);
Serial.begin(9600);
}
void loop()
{
numberSeg(); // Function call for the segment display, Note the () after the function name. This MUST be included.
reaction();
//-=---------------- Print
Serial.println(DispNum, DEC);
Serial.println(count, DEC);
Serial.println(j, DEC);
}
// Below cose is a function for the Segemnt display.
int numberSeg()
{
i = analogRead(POT1)/102;
if (i >= 3 && i <= 9)
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, numbers [i]);
digitalWrite(LATCH, HIGH);
}
confirm(); // Function Call for confirm choice.
}
int confirm()
{
if (!digitalRead(BUTTON1))
{
DispNum = analogRead(POT1)/102, numbers [i]; // Number of tries
}
counter();
}
int counter()
{
count = random(3000, 7000);
for (start = count; start > 0; start--) //
{
delay(1);
}
flash(); // LED FLASH CALLED
}
int flash() // LED flash
{
digitalWrite(LED1, HIGH);
delay(1000);
}
int reaction() // Reaction press
{
while (digitalRead(BUTTON2))
{
digitalWrite(LED1, LOW);
j++;
delay(1);
}
times[DispNum] = j;
}[/code]
I'm only after some advice from some of you more experianced programmers :)
Well, for a start, when posting code, please use the # icon on the editor's toolbar.
Another useful tip is to use the auto format tool in the IDE before posting; this will sort out your eccentric indentation.
The function "confirm" has the return type "int", but doesn't return anything.
The function "reaction" has the return type "int", but doesn't return anything.
The function "numberSeg" has the return type "int", but doesn't return anything.
The function "counter" has the return type "int", but doesn't return anything.
And the function "flash".
Please modify your post, select the code and press the # button. If possible use the CTRL-T option in the IDE to take care of proper identation of the code.
is that better?
AWOL:
Well, for a start, when posting code, please use the # icon on the editor's toolbar.Another useful tip is to use the auto format tool in the IDE before posting; this will sort out your eccentric indentation.
The function "confirm" has the return type "int", but doesn't return anything.
The function "reaction" has the return type "int", but doesn't return anything.
The function "numberSeg" has the return type "int", but doesn't return anything.
The function "counter" has the return type "int", but doesn't return anything.
And the function "flash".
hey
What do you meen it doesnt return anything?
a quick example would help wonders!
If you have a datatype in front of the function name, you have to pass a value back from the function, like this:
int add (int a, int b)
{
int c = a + b;
return c;
}
If you don't care about any results / data from the function, you can put 'void' there instead.
Corvidae:
If you don't care about any results / data from the function, you can put 'void' there instead.
Not to nitpick, but a void does not indicate that one does not care. It explicitly restricts the function to not return anything.
You can easily call a function that return something, and call it without caring about the results / data:
add(1,2); //call a function that return something, without caring
![]()
True. I should probably have been a bit more careful with the wording there. Sorry about that.
Hey again.
I decided in the end to start again, So far i have got this:
#define BUTTON1 10
#define BUTTON2 11
#define POT1 0
#define BUZZER 3
#define LATCH 7
#define CLOCK 8
#define DATA 4
#define LED1 6
const byte numbers[10] = {B10111101, B10101101, B10000101, B10110000, B10011001, B10010010, B10000010, B11111000, B10000000, B10010000 };
int i;
int DispNum;
void setup()
{
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA,OUTPUT);
pinMode(LED1, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println(DispNum);
//------ FUNCTIONS CALLED BELOW --------
numberSeg();
}
//--------------- FUNCTIONS ONLY BELOW PLEASE ---------------------------
int numberSeg()
{
i = analogRead(POT1)/102;
if (i >= 3 && i <= 9)
{
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, numbers[i]);
digitalWrite(LATCH, HIGH);
}
confirm();
}
// Confirm Selection Code
int confirm()
{
if (!digitalRead(BUTTON1))
{
DispNum = i; // Number of tries
}
delay(400);
}
What this does it use the POT and create 10 segments, i only want 3-9 so i set it to that in the NumberSeg.
Once i have picked my number i want to confirm the selection, So made the confirm function.
At the moment it works so when i press BUTTON1 it put out 4 in the serial display, There was no need to return any thing as when i di it broke the code.
is there anyway to stop this looping once i have the number i want?
I simply want it to record the number i have just created and use it to set the test running said ammount of times.
if i dont make sence please shoot me! lol
Thanks people!