Calling QVector in another class error in Qt - qt

I recently have a calling QVector out of index problem.To make my question easy to understand, I created two classes, class A and Class B.
In Class B:
class B
{
public:
B();
QVector<int> mVector;
void setVector()
{
mVector.resize(10);
qDebug() << "size: " << mVector.size();
for(int i = 0; i < 10; i++) {
mVector.push_back(i);
}
qDebug() << "vector:" << mVector;
}
};
And I called the function and QVector in Class A:
B testingB;
qDebug() << testingB.mVector.size();
testingB.setVector();
qDebug() << testingB.mVector;
qDebug() << testingB.mVector.size();
Here is the debug outcome:
0
size: 10
vector: QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
20
I noticed after that the QVector has 20 elements, which should have 10. Why would this happen? Thanks.

Related

Why doesn't arduino nano together with DS3231 make LED strip work as a clock? (I use FastLED library)

For some reason, my program doesn't make the LED clock work properly. I know that the problem is either in the breadboard or the program itself, since with the other program and different circuit everything works well. Everything is fine with the power supply and the connections between the peices of the LED strip are done correctly...
The program doesn’t light up the LEDs hence making it work as a clock; even though a separate program can light up the LEDs themselves, and the other program makes the clock print into the serial monitor the time
THINGS I HAVE ALREADY DONE:
I have already tried using serial prints,
Switching up the LEDs separately using another code (everything worked fine),
Setting up the DS3231 separately and using serial prints to make sure it is working (everything worked fine),
Making sure that the code does not take too much memory,
I have additionally tried different processors to run the code (there are no mistakes in the code itself and avrdude is not displayed)
Can you, please, help me?
These are the connections on the breadboard:
Connections on the breadboard
This is the clock itself:
connection of the parts of the LED strip
You can see the code here:
#include <DS3231.h>
DS3231 rtc(SDA, SCL);
#include <FastLED.h>
#include <Wire.h>
#include <DS1307RTC.h>
#define NUM_LEDS 172 // 6*7*4 +2+2 Number of LED controlled
#define COLOR_ORDER GRB // Define color order for your strip
#define DATA_PIN 12 // Data pin for led comunication D9
CRGB leds[NUM_LEDS]; // Define LEDs strip
const byte digits[10][42] PROGMEM =
{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, // Digit 0
{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1}, // Digit 1
{1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1}, // Digit 2
{0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1}, // Digit 3
{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1}, // Digit 4
{0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}, // Digit 5
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}, // Digit 6
{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1}, // Digit 7
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, // Digit 8
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}}; // Digit 9
int last_digit = 0;
int ledColor = CRGB::White; // Color used (in hex)
void setup(){
//Make the clock work and set its initial parameters
Serial.begin(9600);
rtc.begin();
//rtc.setTime(19, 48, 50); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(3, 6, 2017); // Set the date to the current date
Wire.begin();
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
LEDS.setBrightness(200); // Set initial brightness
}
int GetTime(){
tmElements_t Now;
RTC.read(Now);
//time_t Now = RTC.Now();// Getting the current Time and storing it into a DateTime object
int hour=Now.Hour;
//Serial.println(hour); //ia going to show the hour set to the clock by the other
//Serial.println("--");
int minute=Now.Minute;
//Serial.println(minute); //ia going to show the hour set to the clock by the other
//Serial.println("--");
return (hour*100+minute);
};
// Convert time to array needed for display
void TimeToArray(){
int Now = GetTime(); // Get time
//Serial.print("Time is: ");
//Serial.println(Now);
//delay(1000);
int cursor = 271; // last led number
//dots of the clock
leds[83]=ledColor;
leds[84]=ledColor;
leds[85]=ledColor;
leds[86]=ledColor;
for(int i=1;i<=4;i++){
int digit = Now % 10; // get the last digit of the time starting from the last digit of the minutes
if (i==1){
// Serial.print("the last digit of the minures is : ");Serial.print(digit);Serial.print(" ");
cursor = 129;
for(int k=0; k<=41;k++){
// Serial.print(digits[digit][k]);
if (digits[digit][k]== 1){leds[cursor]=ledColor;}
else if (digits[digit][k]==0){leds[cursor]=0x000000;};
cursor ++;
};
// Serial.println();
}
else if (i==2){
// Serial.print("The first digit of the minutes is : ");Serial.print(digit);Serial.print(" ");
cursor =87;
for(int k=0; k<=41;k++){
// Serial.print(digits[digit][k]);
if (digits[digit][k]== 1){leds[cursor]=ledColor;}
else if (digits[digit][k]==0){leds[cursor]=0x000000;};
cursor ++;
};
// Serial.println();
}
else if (i==3){
// Serial.print("The last digit of the hours is : ");Serial.print(digit);Serial.print(" ");
cursor =41;
for(int k=0; k<=41;k++){
// Serial.print(digits[digit][k]);
if (digits[digit][k]== 1){leds[cursor]=ledColor;}
else if (digits[digit][k]==0){leds[cursor]=0x000000;};
cursor ++;
};
// Serial.println();
}
else if (i==4){
// Serial.print("The first digit of the hours is : ");Serial.print(digit);Serial.print(" ");
cursor =0;
if(digit !=0){
for(int k=0; k<=41;k++){
// Serial.print(digits[digit][k]);
if (digits[digit][k]== 1){leds[cursor]=ledColor;}
else if (digits[digit][k]==0){leds[cursor]=0x000000;};
cursor ++;
};
}
}
Now /= 10;
};
};
void loop() // Main loop
{
GetTime(); // get the time
TimeToArray(); // Get leds array with required configuration
FastLED.show(); // Display leds array
delay(1000);
}
Thank you very much in advance!!
You defined the LED digit patterns in PROGMEM, but did no call pgm_read_byte_near() to access them. As a result, you were loading digit patterns from SRAM, which is zeroed at boot time.
I had to move things around a bit to make a bit more sense of what exactly was going on.
See comment below:
#include <DS3231.h>
#include <FastLED.h>
#include <Wire.h>
#include <DS1307RTC.h>
#define NUM_LEDS 172 // 6*7*4 +2+2 Number of LED controlled
#define COLOR_ORDER GRB // Define color order for your strip
#define DATA_PIN 12 // Data pin for led comunication D9
DS3231 rtc(SDA, SCL);
CRGB leds[NUM_LEDS]; // Define LEDs strip
const byte digits[10][42] PROGMEM =
{ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 0
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // Digit 1
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 2
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 3
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // Digit 4
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // Digit 5
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // Digit 6
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 7
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 8
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}
}; // Digit 9
constexpr int ledColor = CRGB::White; // Color used (in hex)
static void ShowTime() {
//dots of the clock
leds[83] = ledColor;
leds[84] = ledColor;
leds[85] = ledColor;
leds[86] = ledColor;
tmElements_t Now;
RTC.read(Now);
for (int i = 1; i <= 4; ++i) {
int digit = 0;
int cursor = 0;
switch (i)
{
case 1:
cursor = 0;
digit = Now.Hour / 10;
break;
case 2:
cursor = 41;
digit = Now.Hour % 10;
break;
case 3:
cursor = 87;
digit = Now.Minute / 10;
break;
case 4:
cursor = 129;
digit = Now.Minute % 10;
break;
}
// added pgm_read_byte_near() to access PROGMEM data
for (int k = 0; k < 42; ++k, ++cursor)
leds[cursor] = pgm_read_byte_near(&digits[digit][k]) ? ledColor : 0;
}
FastLED.show(); // Display leds array
}
void setup() {
//Make the clock work and set its initial parameters
Serial.begin(9600);
rtc.begin();
//rtc.setTime(19, 48, 50); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(3, 6, 2017); // Set the date to the current date
Wire.begin();
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
LEDS.setBrightness(200); // Set initial brightness
}
void loop() // Main loop
{
ShowTime();
delay(1000);
}

Getting unknown error in SudokuSolver Backtracking problem

I was trying a Backtracking problem (SudokuSolver) I am getting an error which no-one could resolve or even understand. Hence I am seeking help here.
The problem is as follows:
And the main function/part of my code is:
Here my removerow, removecol and removebox functions are removing the numbers which have already occurred in that row, column and sub-box of 3x3 respectively.
The error I am getting is very long that is:
Can someone please help me with this? I'm stuck at it since 2 days. Ready to provide further clarification if needed.
Please find the entire code here: link
I have created a test class, to try understand what your functions are doing - or rather, to make sure they do what I think they do...
import unittest
from Otter.Help_Parth.entire_code import tellbox
from Otter.Help_Parth.entire_code import removerow
from Otter.Help_Parth.entire_code import removecol
from Otter.Help_Parth.entire_code import removebox
class MyTestCase(unittest.TestCase):
def test_tellbox(self):
""" Put each of the 81 x,y combination to tellbox
and retrieve the 81 corresponding box-numbers
assert that the numbers are as expected. """
lst_results = list()
for i in range(9):
for j in range(9):
lst_results.append(tellbox(i, j))
self.assertEqual(lst_results, [0, 0, 0, 3, 3, 3, 6, 6, 6, 0, 0, 0, 3, 3, 3, 6, 6, 6, 0, 0, 0, 3, 3, 3, 6, 6, 6, 1, 1, 1, 4, 4, 4, 7, 7, 7, 1, 1, 1, 4, 4, 4, 7, 7, 7, 1, 1, 1, 4, 4, 4, 7, 7, 7, 2, 2, 2, 5, 5, 5, 8, 8, 8, 2, 2, 2, 5, 5, 5, 8, 8, 8, 2, 2, 2, 5, 5, 5, 8, 8, 8])
def test_removerow(self):
""" Given a partly filled board, a list and an x,y coordinate
try call removerow() and assert that the returned list are as expected. """
board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[7, 8, 9, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x, y = 2, 4
res = removerow(l, board, x, y)
self.assertEqual([2, 3, 4, 5, 6], res)
def test_removecol(self):
""" Given a partly filled board, a list and an x,y coordinate
try call removecol() and assert that the returned list are as expected. """
board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[7, 8, 9, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x, y = 2, 4
res = removecol(l, board, x, y)
self.assertEqual([1, 2, 3, 4, 6, 7, 9], res)
def test_removebox(self):
""" Given a partly filled board, a list and an x,y coordinate
try call removebox() and assert that the returned list are as expected. """
board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[7, 8, 9, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x, y = 2, 4
bno = tellbox(x, y)
res = removebox(l, board, x, y, bno)
self.assertEqual([2, 3], res)
if __name__ == '__main__':
unittest.main()
It all goes quite well, until removebox().
If you can read and understand my test_removebox(self) Are you kind to confirm that, under the given circumstances, it should in fact return [2, 3]?
Otherwise, please tell what it should return, and why?
When I run the test class, it returns the following:
NB: To be able to run the test class against your code, you need to change it like this:
if __name__ == '__main__':
board = [[ int(ele) for ele in input().split() ] for i in range(9)]
ans=solveSudoku(board,0,0)
if ans:
print('true')
else:
print('false')
Both your code-sample and the output seems incomplete.
We don't know your variables, so I assume 'board' is a list of 81 integers, but what is the mysterious (and newer used) boxl?
And why would you set board[x][y] = 0 (in the line with the comment), when you have just gone through the trouble of setting it = ele?
We would be able to help you better, if you provided the entire code, in a run-able form.
Sorry! I just realized that I answered to the wrong post!

How to calculate the register reset value?

I have register defined inside a register model as shown below
virtual function void build();
this.PM_CAP_ID = uvm_reg_field::type_id::create("PM_CAP_ID",,get_full_name());
this.PM_CAP_ID.configure(this, 8, 0, "RO", 0, 8'h1, 1, 0, 1);
this.PM_NEXT_POINTER = uvm_reg_field::type_id::create("PM_NEXT_POINTER",,get_full_name());
this.PM_NEXT_POINTER.configure(this, 8, 8, "RO", 0, 8'h50, 1, 0, 1);
this.PM_SPEC_VER = uvm_reg_field::type_id::create("PM_SPEC_VER",,get_full_name());
this.PM_SPEC_VER.configure(this, 3, 16, "RO", 0, 3'h3, 1, 0, 0);
this.PME_CLK = uvm_reg_field::type_id::create("PME_CLK",,get_full_name());
this.PME_CLK.configure(this, 1, 19, "RO", 0, 1'h0, 1, 0, 0);
this.Reserved_20_20 = uvm_reg_field::type_id::create("Reserved_20_20",,get_full_name());
this.Reserved_20_20.configure(this, 1, 20, "RO", 0, 1'h0, 0, 0, 0);
uvm_resource_db#(bit)::set({"REG::", Reserved_20_20.get_full_name()}, "NO_REG_TESTS", 1, this);
uvm_resource_db#(bit)::set({"REG::", Reserved_20_20.get_full_name()}, {Reserved_20_20.get_full_name(), "_NO_REG_TESTS"}, 1, this);
this.DSI = uvm_reg_field::type_id::create("DSI",,get_full_name());
this.DSI.configure(this, 1, 21, "RO", 0, 1'h0, 1, 0, 0);
this.AUX_CURR = uvm_reg_field::type_id::create("AUX_CURR",,get_full_name());
this.AUX_CURR.configure(this, 3, 22, "RO", 0, 3'h7, 1, 0, 0);
this.D1_SUPPORT = uvm_reg_field::type_id::create("D1_SUPPORT",,get_full_name());
this.D1_SUPPORT.configure(this, 1, 25, "RO", 0, 1'h1, 1, 0, 0);
this.D2_SUPPORT = uvm_reg_field::type_id::create("D2_SUPPORT",,get_full_name());
this.D2_SUPPORT.configure(this, 1, 26, "RO", 0, 1'h1, 1, 0, 0);
this.PME_SUPPORT = uvm_reg_field::type_id::create("PME_SUPPORT",,get_full_name());
this.PME_SUPPORT.configure(this, 5, 27, "RO", 1, 5'h1b, 1, 0, 0);
uvm_resource_db#(bit)::set({"REG::", PME_SUPPORT.get_full_name()}, "NO_REG_BIT_BASH_TEST", 1, this);
uvm_resource_db#(bit)::set({"REG::", PME_SUPPORT.get_full_name()}, {PME_SUPPORT.get_full_name(), "_NO_REG_BIT_BASH_TEST"}, 1, this);
endfunction: build
In configure, the 6th position from the left gives the reset value. How to combine all these reset values of individual fields and arrive at a final value?
You can use the function get_reset() in the uvm_reg:
For example:
<reg_instance>.get_reset();

What is difference Qt.matrix4x4 and QMatrix4x4?

I am learning the Qt 3D module. I found the Qt.matrix4x4 provide different API than QMatrix4x4 in C++ side (by using F1 check the API doc).
look likes Qt.matrix4x4 don't have such scale or rotation operations. even not in autocomplete.
BUT! these functions do exist and work!
var m = Qt.matrix4x4(1, 0, 0, -0.5, 0, 1, 0, 0, 0, 0, 1, 0.5, 0, 0, 0, 1)
m.scale(2)
console.log(m)
// qml: QMatrix4x4(2, 0, 0, -0.5, 0, 2, 0, 0, 0, 0, 2, 0.5, 0, 0, 0, 1)
// the type shows QMatrix4x4
https://doc.qt.io/qt-5/qmatrix4x4.html vs https://doc.qt.io/qt-5/qml-matrix4x4.html
I understand what these is, But why the API show different in API doc and autocomplete?
It is interesting to note that in Qt6 the documentation appears different than Qt5. In Qt6 we see clearly there are methods for constructing the matrix4x4 basic type from a vector and for multiplying them with vectors.
// translate(vector3d)
var m = Qt.matrix4x4();
m.translate(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 0, 0, 0, 1)
// matrix multiplication
var m = Qt.matrix4x4();
m.translate(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 0, 0, 0, 1)
// applying a matrix to a vector
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.vector3d(5,6,7);
var c = a.times(b);
console.log(c.toString()); // QVector3D(0.155556, 0.437037, 0.718518)
Note I tried all of the above in Qt5.15.6 but it doesn't work. So it appears that support for this type really kicks in at Qt6.
For further information see: https://doc.qt.io/qt-6/qml-matrix4x4.html

Is it possible to store Arduino data for sketch in plain text onboard Arduino?

I created this arduino sketch and it works fine, what happens is you hold a button down and a pattern of lights plays. You can see my patterns at the bottom. Each button has its own pattern to play when its held.
So this works fine, but I have a question. I'm uneasy about this because I feel that its better practice to keep my patterns outside the program. I want to know, can I store the patterns in a text file that also gets loaded onto the arduino? Is there anyway to put the patterns in a text file, and just read them all onboard the arduino?
const int buttonPin[5] = {8,9,10,11,12};
const int LEDPin[5] = {2,3,4,5,6};
int timer =0;
int millisBegin=0;
boolean reset=true;
boolean run[5] = {false,false,false,false,false};
boolean buttonOn = false;
void setup(){
Serial.begin(57600);
pinMode(buttonPin[0], INPUT);
pinMode(buttonPin[1], INPUT);
pinMode(buttonPin[2], INPUT);
pinMode(buttonPin[3], INPUT);
pinMode(buttonPin[4], INPUT);
pinMode(LEDPin[0], OUTPUT);
pinMode(LEDPin[1], OUTPUT);
pinMode(LEDPin[2], OUTPUT);
pinMode(LEDPin[3], OUTPUT);
pinMode(LEDPin[4], OUTPUT);
}
void loop()
{
for (int x=0; x<6; x++)
{
if (digitalRead(buttonPin[x]) == HIGH)
{
run[x] = true;
}
}
if (run[0] == true)
{
buttonOn = pattern1();
if (buttonOn == false)
{
run[0] = false;
}
}
if (run[1] == true)
{
buttonOn = pattern2();
if (buttonOn == false)
{
run[1] = false;
}
}
if (run[2] == true)
{
buttonOn = pattern3();
if (buttonOn == false)
{
run[2] = false;
}
}
if (run[3] == true)
{
buttonOn = pattern4();
if (buttonOn == false)
{
run[3] = false;
}
}
if (run[4] == true)
{
buttonOn = pattern5();
if (buttonOn == false)
{
run[4] = false;
}
}
}
boolean light(int button, int pattern[][6])
{
while (digitalRead(buttonPin[button])==LOW)
{
reset = true;
for (int x=0; x<5; x++){
digitalWrite(LEDPin[x],LOW);
}
buttonOn = false;
return buttonOn;
}
while (digitalRead(buttonPin[button])==HIGH)
{
if (reset == true)
{
millisBegin = millis();
reset = false;
Serial.println("reset!");
}
timer = millis() - millisBegin;
for (int x=0; x<10; x++) //Pattern amount
{
for (int y=0; y<5; y++) //Lights
{
//if else runs pattern with correct time code
if (timer>pattern[x][5]&&timer<pattern[x+1][5])
{
if (pattern[x][y]==1)
{
digitalWrite(LEDPin[y], HIGH);
}
else
{
digitalWrite(LEDPin[y], LOW);
}
}
}
}
Serial.println(timer);
if (timer > pattern[10][5]){
reset = true;
Serial.println("Over timer!");
}
}
}
boolean pattern1()
{
int pattern[11][6] = {
{0, 0, 0, 0, 0, 100}, //0
{1, 1, 0, 1, 1, 1200}, //1
{0, 0, 1, 0, 0, 2200}, //2
{1, 1, 0, 1, 1, 3200}, //3
{0, 0, 0, 0, 0, 4200}, //4
{1, 1, 1, 1, 1, 5000}, //5
{0, 0, 1, 0, 0, 6000}, //6
{1, 0, 0, 0, 0, 7000}, //7
{0, 1, 0, 1, 1, 8000}, //8arduin
{0, 0, 1, 0, 1, 9000}, //9
{0, 0, 0, 0, 0, 10000}}; //
buttonOn = light(0,pattern);
return buttonOn;
}
boolean pattern2()
{
int pattern[11][6] = {
{1, 0, 1, 0, 0, 100}, //0
{0, 1, 1, 0, 1, 180}, //1
{1, 0, 0, 0, 0, 230}, //2
{0, 1, 0, 1, 1, 340}, //3
{1, 0, 0, 1, 0, 450}, //4
{0, 0, 1, 1, 1, 500}, //5
{0, 0, 1, 0, 0, 550}, //6
{1, 0, 0, 0, 0, 600}, //7
{0, 1, 0, 1, 1, 680}, //8
{0, 0, 1, 0, 1, 800}, //9
{0, 0, 0, 0, 0, 900}}; //
buttonOn = light(1,pattern);
return buttonOn;
}
boolean pattern3()
{
int pattern[11][6] = {
{1, 1, 1, 1, 1, 100}, //0
{0, 0, 0, 0, 0, 180}, //1
{1, 1, 1, 1, 1, 230}, //2
{0, 0, 0, 0, 0, 340}, //3
{1, 1, 1, 1, 1, 450}, //4
{0, 0, 0, 0, 0, 500}, //5
{1, 1, 1, 1, 1, 550}, //6
{0, 0, 0, 0, 0, 600}, //7
{1, 1, 1, 1, 1, 680}, //8
{0, 0, 0, 0, 0, 800}, //9
{0, 0, 0, 0, 0, 810}}; //
buttonOn = light(2,pattern);
return buttonOn;
}
boolean pattern4()
{
int pattern[11][6] = {
{0, 0, 0, 0, 0, 100}, //0
{0, 0, 0, 0, 1, 500}, //1
{0, 0, 0, 1, 1, 800}, //2
{0, 0, 1, 1, 1, 900}, //3
{1, 1, 1, 1, 1, 1000}, //4
{1, 1, 1, 1, 1, 1100}, //5
{1, 1, 1, 1, 0, 1200}, //6
{1, 1, 1, 0, 0, 1300}, //7
{1, 1, 0, 0, 0, 1400}, //8
{1, 0, 0, 0, 0, 1500}, //9
{0, 0, 0, 0, 0, 1600}}; //
buttonOn = light(3,pattern);
return buttonOn;
}
boolean pattern5()
{
int pattern[11][6] = {
{0, 1, 0, 1, 0, 100}, //0
{1, 0, 1, 0, 1, 500}, //1
{0, 1, 0, 1, 0, 800}, //2
{1, 0, 1, 0, 1, 900}, //3
{0, 1, 0, 1, 0, 1000}, //4
{1, 0, 1, 0, 1, 1100}, //5
{0, 1, 0, 1, 0, 1200}, //6
{1, 0, 1, 0, 1, 1300}, //7
{0, 1, 0, 1, 0, 1400}, //8
{1, 0, 1, 0, 1, 1500}, //9
{0, 1, 0, 1, 0, 1600}}; //
buttonOn = light(4,pattern);
return buttonOn;
}
The Arduino boards do not have external storage devices where files can be stored. Of course there are shields with a SD card slot which you can add to your device. This would be overkill however.
But the Arduino has a builtin EEPROM which you can read/write using the EEPROM library.
My suggestion would be:
Extend your program to read and write to USB via Serial. You can then invent some simple text-based commands which allow you to send new patterns to the Arduino.
When the Arduino receives new patterns, it stores them in the EEPROM.
In normal operation mode the Arduino reads the patterns from EEPROM and displays them.
This way you can use your Arduino "standalone" and program it ad-hoc using any computer.

Resources