Hi, i have a variable called count which i can increase and decrease using 2 different buttons. i would like to know how i can store 4 different numbers from the count variable in an array.
for example, if count is 3 i will click the button 'store' then if count is 6, i will click 'store', then if it's 8 i will 'store' and 9.
so i have a combination of numbers such as 3 6 8 9. then button 'test' will determine if these 4 numbers stored in an array match the variable- pin = 3689;
if it does match the led will turn on if it doesn't the led will blink. then how do i reset the array back to 0 to start the combination again.
byte theArray[4] = 0; //declares an array with 4 positions and sets them all to zero
declare a variable to use as an index to the array
byte arrayIndex = 0; //index to the array
Now, when you want to save a value do this
theArray[arrayIndex] = theValue;
arrayIndex++; //increment the array index to the next position
if (arrayIndex == 4)
{
//the array has 4 entries numbered from 0 to 3 and is now full
//code here to test the array entries
}
Set the array entries back to zero with a for loop
#include <MultiFuncShield.h>
#include <TimerOne.h>
#include <Wire.h>
const int Button = A1;
const int Button1 = A2;
const int Button2 = A3;
const int Point1 = 6;
const int Point2 = 9;
const int Switch = 5;
int ButtonState = 0;
int Value = 0;
int On = 0;
int ButtonState1 = 0;
int Value1 = 0;
int On1 = 0;
int ButtonState2 = 0;
int Value2 = 0;
int On2 = 0;
int Count = 0;
int Count_Value = 0;
int C_State;
int L_State;
byte theArray[4] = {0};
byte arrayIndex = {0};
void setup()
{
Timer1.initialize();
MFS.initialize(&Timer1);
Serial.begin(9600);
pinMode(Button, INPUT_PULLUP);
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Point1, INPUT);
pinMode(Point2, INPUT);
pinMode(Switch, INPUT);
MFS.write("----");
L_State = digitalRead(Point1);
}
void loop()
{
Passive();
////here is the array
if(digitalRead(Switch) == HIGH)
{
theArray[arrayIndex] = Count;
arrayIndex++;
if (arrayIndex == 4)
{
if (Count_Value == 2468)
{
MFS.write("On");
delay(500);
}
else
{
MFS.write("SOS");
delay(500);
}
}
}
for (arrayIndex = 0; arrayIndex < 4; arrayIndex++)
{
theArray[arrayIndex] = 0;
}
}
void Passive ()
{
C_State = digitalRead(Point1);
if (C_State != L_State)
{
if (digitalRead(Point2) != C_State)
{
Count --;
}
else
{
Count ++;
}
Serial.print("Position: ");
Serial.println(Count);
MFS.write(Count);
}
L_State = C_State;
}
so i have this code, when the button is clicked the count value is stored in the array. but i am unable to identify the 'switch' is shifting the vale to the array or not
As you never change the value of Count_Value this is never going to be true.
If you want to test whether the array contains 2 4 6 8 you can either test each entry separately to see whether it equals the corresponding digit or do some maths on the values to see whether the result is 2468
Read what Paul said, You need to detect when the button becomes pressed, not when it is pressed
i have no understanding of how to detect when it is pressed i have only been shown how to press it. could you advise me on how to detect when the button is pressed to store the value of count to the array
i cannot understand how to insert functions to store the value of count to the array 4 times then test against 'theArray' which is 8 4 6 2. if these 4 numbers are in the array i want 1 led on if they are not then i want the other led. how do i do this. sorry im a beginner.
i cannot understand how to insert functions to store the value of count to the array 4 times then test against 'theArray' which is 8 4 6 2.
First fix the problem of reading the button when it becomes pressed
To compare the digits, say 8, 4, 6 and 2 held in the array in position 0, 1, 2 and 3 with the actual number 8462 you could do this
int guess = (1000 * theArray[0]) + (100 * theArray[1] + (10 * theArray[2]) + theArray[3]/code]
Then compare guess with the code number
I suggest that you try the calculation yourself with pencil and paper so that you understand fully how it works
so i have been working on my code using your advice. i have managed to get button 1 to count up and button 2 to count down.
i am trying to use the byte array 2 store 1 value using button 3 to add the value and if the value is true against the byte then the LED is on, if false LED is off. The byte testing part is under Void Test
Could you help me in where i am going wrong?
#include <MultiFuncShield.h>
#include <TimerOne.h>
#include <Wire.h>
int CountDown = A2;
int CountUp = A1;
int Test = A3;
int LED1 = 13;
int LED4 = 10;
int ButtonPressCount = 0;
int ButtonState = 0;
int LastBState = 0;
int ButtonPressCount1 = 0;
int ButtonState1 = 0;
int LastBState1 = 0;
int ButtonPressCount2 = 0;
int ButtonState2 = 0;
int LastBState2 = 0;
int Count = 0;
byte num1[1] = {5};
byte arrayIndex = 0;
void setup() {
Timer1.initialize();
MFS.initialize(&Timer1);
MFS.write("----");
Serial.begin(9600);
pinMode(CountDown, INPUT);
pinMode(CountUp, INPUT);
pinMode(Test, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop() {
Countup();
Countdown();
Store();
}
void Countup()
{
ButtonState = digitalRead(CountUp);
if (ButtonState != LastBState)
{
if (ButtonState == LOW)
{
Count++;
}
delay(100);
}
LastBState = ButtonState;
MFS.write(Count);
}
void Countdown()
{
ButtonState1 = digitalRead(CountDown);
if (ButtonState1 != LastBState1)
{
if (ButtonState1 == LOW)
{
Count--;
}
delay(100);
}
LastBState1 = ButtonState1;
MFS.write(Count);
}
void Store()
{
ButtonState2 = digitalRead(Test);
if (ButtonState2 != LastBState2)
{
if (ButtonState2 == LOW)
{
num1[arrayIndex] = Count;
arrayIndex++;
if (arrayIndex == 1)
{
digitalWrite(LED1, LOW);
}
else
{
digitalWrite(LED1, HIGH);
}
}
delay(100);
}
LastBState2 = ButtonState2;
for (arrayIndex = 0; arrayIndex < 2; arrayIndex++)
{
num1[arrayIndex] = 0;
}
}
i am trying to use the byte array 2 store 1 value using button 3 to add the value and if the value is true against the byte then the LED is on, if false LED is off. The byte testing part is under Void Test
I don't quite understand what you are trying to do
To start with you do not have a function named Test
You do not test the contents if the array anywhere in the program
byte num1[1] = {5};
What is the point of an array with only one level ?
You set arrayIndex to 0 then later in the program you increment and turn on the LED if it is set to 1. You use it use it in a for loop each time through loop() which leaves its value as 1 so it will never be 1 again after you increment it.