Related
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);
}
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.
I need to display a quadrilateral mesh in javafx each mesh face having 4 points i have tried some triangle mesh examples from fxyz library, but not sure how does it work for quadrilaterals, Can someone help pointing to examples in javafx for quadrilateral mesh.
The 3DViewer project that is available at the OpenJFX repository, contains already a PolygonalMesh implementation, that allows any number of points per face, so any polygon can be a face.
You can use them that mesh implementation in a PolygonMeshView, instead of the regular MeshView.
Since a triangle is a valid polygon, any TriangleMesh can be easily used as a PolygonMesh.
For instance, the CuboidMesh from FXyz library has the following implementation assuming a PolygonMesh:
private PolygonMesh getTriangleMesh(float width, float height, float depth) {
float L = 2f * width + 2f * depth;
float H = height + 2f * depth;
float hw = width/2f, hh = height/2f, hd = depth/2f;
float[] points = new float[] {
hw, hh, hd, hw, hh, -hd,
hw, -hh, hd, hw, -hh, -hd,
-hw, hh, hd, -hw, hh, -hd,
-hw, -hh, hd, -hw, -hh, -hd
};
float[] texCoords = new float[] {
depth / L, 0f, (depth + width) / L, 0f,
0f, depth / H, depth / L, depth / H,
(depth + width) / L, depth / H, (2f * depth + width) / L, depth/H,
1f, depth / H, 0f, (depth + height) / H,
depth / L, (depth + height)/H, (depth + width) / L, (depth + height) / H,
(2f * depth + width) / L, (depth + height) / H, 1f, (depth + height) / H,
depth / L, 1f, (depth + width) / L, 1f
};
int[][] faces = new int[][] {
{0, 8, 2, 3, 1, 7}, {2, 3, 3, 2, 1, 7},
{4, 9, 5, 10, 6, 4}, {6, 4, 5, 10, 7, 5},
{0, 8, 1, 12, 4, 9}, {4, 9, 1, 12, 5, 13},
{2, 3, 6, 4, 3, 0}, {3, 0, 6, 4, 7, 1},
{0, 8, 4, 9, 2, 3}, {2, 3, 4, 9, 6, 4},
{1, 11, 3, 6, 5, 10}, {5, 10, 3, 6, 7, 5}
};
int[] smooth = new int[] {
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6
};
PolygonMesh mesh = new PolygonMesh(points, texCoords, faces);
mesh.getFaceSmoothingGroups().addAll(smooth);
return mesh;
}
This gives the following result:
private double mouseOldX, mouseOldY = 0;
private final Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
private final Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
#Override
public void start(Stage primaryStage) {
PolygonMeshView meshView = new PolygonMeshView(getTriangleMesh(100, 150, 200));
meshView.setDrawMode(DrawMode.LINE);
meshView.setCullFace(CullFace.NONE);
meshView.setMaterial(new PhongMaterial(Color.LIGHTYELLOW));
Scene scene = new Scene(new Group(meshView), 500, 300, true, SceneAntialiasing.BALANCED);
scene.setOnMousePressed(event -> {
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
scene.setOnMouseDragged(event -> {
rotateX.setAngle(rotateX.getAngle() - (event.getSceneY() - mouseOldY));
rotateY.setAngle(rotateY.getAngle() + (event.getSceneX() - mouseOldX));
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setNearClip(0.1);
camera.setFarClip(10000.0);
camera.getTransforms().addAll(rotateX, rotateY, new Translate(-250, -150, 0));
scene.setCamera(camera);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
But if we combine the triangles of faces in same sides of the prism, we can easily generate the quadrilateral faces. Note that points and texCoords remain the same:
private PolygonMesh getQuadrilateralMesh(float width, float height, float depth) {
float L = 2f * width + 2f * depth;
float H = height + 2f * depth;
float hw = width/2f, hh = height/2f, hd = depth/2f;
float[] points = new float[] {
hw, hh, hd, hw, hh, -hd,
hw, -hh, hd, hw, -hh, -hd,
-hw, hh, hd, -hw, hh, -hd,
-hw, -hh, hd, -hw, -hh, -hd
};
float[] texCoords = new float[] {
depth / L, 0f, (depth + width) / L, 0f,
0f, depth / H, depth / L, depth / H,
(depth + width) / L, depth / H, (2f * depth + width) / L, depth/H,
1f, depth / H, 0f, (depth + height) / H,
depth / L, (depth + height)/H, (depth + width) / L, (depth + height) / H,
(2f * depth + width) / L, (depth + height) / H, 1f, (depth + height) / H,
depth / L, 1f, (depth + width) / L, 1f
};
int[][] faces = new int[][] {
{0, 8, 2, 3, 3, 2, 1, 7},
{4, 9, 5, 10, 7, 5, 6, 4},
{0, 8, 1, 12, 5, 13, 4, 9},
{2, 3, 6, 4, 7, 1, 3, 0},
{0, 8, 4, 9, 6, 4, 2, 3},
{1, 11, 3, 6, 7, 5, 5, 10}
};
int[] smooth = new int[] {
1, 2, 3, 4, 5, 6
};
PolygonMesh mesh = new PolygonMesh(points, texCoords, faces);
mesh.getFaceSmoothingGroups().addAll(smooth);
return mesh;
}
That will be used as:
#Override
public void start(Stage primaryStage) {
PolygonMeshView meshView = new PolygonMeshView(getQuadrilateralMesh(100, 150, 200));
...
}
giving the expected result:
Note that for this sample each face is using points and texture indices, but you normal indices could be added as well.
I am trying to learn how to use QPainter with a QGLFramebufferObject. When I try to display the texture in a QGLWidget, it is not visible. (complete code below)
The end goal is to use QPainter to draw text onto textures and then alpha blend the texture overtop of the 2D line geometry.
texture.pro
QT += core gui widgets opengl
TARGET = test
TEMPLATE = app
SOURCES = main.cpp
HEADERS = main.h
main.h
#include <QGLWidget>
#include <QGLFunctions>
class glview : public QGLWidget, protected QGLFunctions
{
Q_OBJECT
public:
explicit glview(QWidget *parent = 0);
~glview();
QSize sizeHint() const;
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
quint32 vbo_id[2], texture_id;
};
main.cpp
#include <QApplication>
#include <QGLFramebufferObject>
#include <QPainter>
#include "main.h"
struct vrtx {
GLint x;
GLint y;
GLubyte r;
GLubyte g;
GLubyte b;
}__attribute__((packed)) line_geo[] = {
// x, y, r, g, b
{1, 1, 255, 0, 0},
{1, 2, 0, 255, 0},
{1, 2, 0, 255, 0},
{2, 2, 255, 0, 0},
{2, 2, 255, 0, 0},
{2, 1, 0, 255, 0},
{2, 1, 0, 255, 0},
{1, 1, 255, 0, 0},
};
struct txtr_vrtx {
GLint x;
GLint y;
GLint tx;
GLint ty;
}__attribute__((packed)) txtr_geo[] = {
// x, y, tx,ty
{3, 1, 0, 0},
{3, 2, 0, 1},
{4, 2, 1, 1},
{4, 1, 1, 0},
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
glview widget;
widget.show();
return app.exec();
}
glview::glview(QWidget *parent) : QGLWidget(parent)
{
}
glview::~glview()
{
}
QSize glview::sizeHint() const
{
return QSize(500, 300);
}
void glview::initializeGL()
{
initializeGLFunctions();
qglClearColor(Qt::white);
glGenBuffers(2, vbo_id);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(line_geo), line_geo, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(txtr_geo), txtr_geo, GL_STATIC_DRAW);
QGLFramebufferObject fbo(100, 100, QGLFramebufferObject::CombinedDepthStencil/*GL_TEXTURE_2D*/);
fbo.bind();
texture_id = fbo.texture();
QPainter painter(&fbo);
painter.fillRect(0, 0, 100, 100, Qt::blue);
painter.end();
fbo.release();
}
void glview::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void glview::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0, 5, 0, 3, -1, 1);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
glVertexPointer(2, GL_INT, sizeof(struct vrtx), 0);
glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(struct vrtx), ((char*)NULL + 8));
glDrawArrays(GL_LINES, 0, sizeof(line_geo) / sizeof(struct vrtx));
//glColor4ub(0, 0, 255, 255);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[1]);
glBindTexture(GL_TEXTURE_2D, texture_id);
glVertexPointer(2, GL_INT, sizeof(struct txtr_vrtx), 0);
glTexCoordPointer(2, GL_INT, sizeof(struct txtr_vrtx), ((char*)NULL + 8));
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_2D);
//glDisable(GL_BLEND);
glFlush();
}
The QGLFramebufferObject instance is destroyed when leaving initializeGL(). This results in deleting the texture too. You need to keep the QGLFramebufferObject alive until the texture is no longer needed.
Here is the corrected code. Also with alpha bending.
main.h
#include <QGLWidget>
#include <QGLFunctions>
#include <QGLFramebufferObject>
#include <QFont>
class glview : public QGLWidget, protected QGLFunctions
{
Q_OBJECT
public:
explicit glview(QWidget *parent = 0);
~glview();
QSize sizeHint() const;
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
QGLFramebufferObject *fbo;
QFont font;
quint32 vbo_id[2], texture_id;
};
main.cpp
#include <QApplication>
#include <QPainter>
#include "main.h"
struct vrtx {
GLint x;
GLint y;
GLubyte r;
GLubyte g;
GLubyte b;
}__attribute__((packed)) line_geo[] = {
// x, y, r, g, b
{1, 1, 255, 0, 0},
{1, 2, 0, 255, 0},
{1, 2, 0, 255, 0},
{2, 2, 255, 0, 0},
{2, 2, 255, 0, 0},
{2, 1, 0, 255, 0},
{2, 1, 0, 255, 0},
{1, 1, 255, 0, 0},
};
struct txtr_vrtx {
GLint x;
GLint y;
GLint tx;
GLint ty;
}__attribute__((packed)) txtr_geo[] = {
// x, y, tx,ty
{3, 1, 0, 0},
{3, 2, 0, 1},
{4, 2, 1, 1},
{4, 1, 1, 0},
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
glview widget;
widget.show();
return app.exec();
}
glview::glview(QWidget *parent) : QGLWidget(parent)
{
font.setFamily("Helvetica");
}
glview::~glview()
{
delete fbo;
}
QSize glview::sizeHint() const
{
return QSize(500, 300);
}
void glview::initializeGL()
{
initializeGLFunctions();
qglClearColor(Qt::white);
glGenBuffers(2, vbo_id);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(line_geo), line_geo, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(txtr_geo), txtr_geo, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); // must unbind for QPainter
fbo = new QGLFramebufferObject(100, 100, GL_TEXTURE_2D);
fbo->bind();
texture_id = fbo->texture();
QPainter painter(fbo);
painter.setPen(Qt::blue);
font.setPointSize(20);
painter.setFont(font);
painter.drawText(0, 60, "FBO");
painter.end();
fbo->release();
}
void glview::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void glview::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0, 5, 0, 3, -1, 1);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
glVertexPointer(2, GL_INT, sizeof(struct vrtx), 0);
glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(struct vrtx), ((char*)NULL + 8));
glDrawArrays(GL_LINES, 0, sizeof(line_geo) / sizeof(struct vrtx));
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[1]);
glBindTexture(GL_TEXTURE_2D, texture_id);
glVertexPointer(2, GL_INT, sizeof(struct txtr_vrtx), 0);
glTexCoordPointer(2, GL_INT, sizeof(struct txtr_vrtx), ((char*)NULL + 8));
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glFlush();
}
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.