Good afternoon, I have been working with graphics in Qt relatively recently, I ran into such a problem. For reasons that are unclear to me,
PART of the QGraphicsItem is not drawn under the selection, and after releasing the selection appears only when calling update method, mousePressed does not work in these disappearing zones too (see screen 2), but if you select the same item in a bit other place, everything is OK, moreover, at some locations this error disappears (see screen 3) blue rectangles these are boundingRect, red outline shape, gray rectangles proxyWidget nested in bright green GraphicsItem. thank you in advanceinJust Build
Selection break Same Instance, Just Move widgets a bit, and no error
HPP
#include <QGraphicsLineItem>
#include <QMoveEvent>
class pin;
class customGraphicsview;
class connector;
class Dot;
#include"dot.h"
#include"pin.h"
#include"connector.h"
#include<QGraphicsItem>
#include <QPainter>
#include<QPointF>
#include<QDebug>
#include<QColor>
class Whire : public QGraphicsItem
{
public:
Whire(pin *nitem1, pin *nitem2,customGraphicsview*gv);
void Update(bool OneMore=false);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
pin *pin1,*pin2;
void collisions();
void DotDot(Whire* w);
void collisionfix(Whire* w);
void collisionfix(connector* cn);
void mousePressEvent(QGraphicsSceneMouseEvent *e) override;
void colChange(QColor co);
QColor color();
QPointF NeartoPindot(pin* p,bool bl);
QPointF NeartoPin(pin* p);
~Whire();
private:
QColor col;
bool selected=false;
QVector<Dot*>points;
void drawLine(Dot* p1, Dot* p2, Dot* p3, int& r, QPainter* p,bool last=false);
void drawLine(Dot* p1, Dot* p2,QPainter* p);
QRectF boundingRect() const override;
QPainterPath shape() const override;
customGraphicsview* gview;
bool p2c=false,p3c=false;
};
CPP
#include "whire.h"
#include"customviewmap.h"
#include<qgraphicsitem.h>
Whire::Whire(pin *nitem1, pin *nitem2,customGraphicsview* gv): QGraphicsItem()
{
pin1=nitem1;
pin2=nitem2;
gview=gv;
if(dynamic_cast<customviewMap*>(gview->view)!=nullptr)
{
dynamic_cast<customviewMap*>(gview->view)->Add(this);
}
gview->scene()->addItem(this);
col=Qt::darkGray;
points=QVector<Dot*>();
pin1->addWhire(this);
pin2->addWhire(this);
points.clear();
Dot* p = new Dot(pin1->whirepos(pin2), 0,gview->scene());
points.append(p);
p = new Dot(pin2->whirepos(pin1), 1, gview->scene());
points.append(p);
p = new Dot(((float)(points[0]->pos().x() + points.last()->pos().x()) / 2), points[0]->pos().y(), 1, gview->scene());
points.insert(1, p);
p = new Dot(((float)(points[0]->pos().x() + points.last()->pos().x()) / 2), points.last()->pos().y(), 0, gview->scene());
points.insert(2, p);
collisions();
}
void Whire::Update(bool colfix)
{
prepareGeometryChange();
Dot* p = new Dot(pin1->whirepos(pin2), 0,scene());
delete points[0];
points[0] = p;
p = new Dot(pin2->whirepos(pin1), 1, scene());
delete points.last();
points.last() = p;
p = new Dot(((float)(points[0]->pos().x() + points.last()->pos().x()) / 2), points[0]->pos().y(), 1, scene());
delete points[1];
points[1] = p;
p = new Dot(((float)(points[0]->pos().x() + points.last()->pos().x()) / 2), points.last()->pos().y(), 0, scene());
delete points[points.count() - 2];
points[points.count() - 2] = p;
for (int i = 2; i < points.count()-2; i++)
{
delete points[i];
points.remove(i);
}
this->update();
if(colfix)
{
collisions();
}
}
void Whire::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//qDebug()<<points.count();
/*
int radius=0;
painter->setPen(QPen(col,8));
for(int i=0;i<points.count()-3;i++)
{
drawLine(points[i],points[i+1],points[i+2],radius,painter);
}
if(points.count()>2)
drawLine(points[points.count()-3],points[points.count()-2],points[points.count()-1],radius,painter,true);
*/
for (int i = 0; i < points.count()-1; i++)
drawLine(points[i], points[i + 1],painter);
painter->setPen(QPen(Qt::red,Qt::DashDotDotLine));
painter->setBrush(QBrush(Qt::transparent));
painter->drawPath(shape());
painter->setPen(QPen(Qt::darkBlue, Qt::DashLine));
painter->drawRect(boundingRect());
}
void Whire::collisions()
{
QList<Whire*>colWhires=QList<Whire*>();
QList<connector*>colConn=QList<connector*>();
Whire* wr;
connector* cn;
RefreshWGraphicsRectItem* RGI;
QList<QGraphicsItem *> items = collidingItems();
foreach (QGraphicsItem *item, items)
{
RGI=dynamic_cast<RefreshWGraphicsRectItem*>(item);
if(RGI!=nullptr)
{
break;
}
wr = dynamic_cast<Whire*>(item);
if(wr != nullptr)
{
colWhires.append(wr);
break;
}
cn = dynamic_cast<connector*>((dynamic_cast<QGraphicsProxyWidget*>(item))->widget());
if( cn != nullptr)
{
if(cn!=pin1->connectr()&&cn!=pin2->connectr())
{
colConn.append(cn);
}
}
}
foreach(Whire* wr, colWhires)
{
collisionfix(wr);
}
foreach(connector* cn,colConn)
{
//collisionfix(cn);
}
if(abs(pin1->whirepos().x()-pin2->whirepos().x())<pin1->connectr()->size().width())
{
if(pin1->connectr()->pos().x()<=pin2->connectr()->pos().x())
{
points[0]= new Dot(QPointF(pin1->whirepos(QPointF(pin1->connectr()->pos().x()-pin1->connectr()->size().width(),pin1->connectr()->pos().y()))),points[0]->inH, scene());
}
else
{
points[0]= new Dot(QPointF(pin1->whirepos(QPointF(pin1->connectr()->pos().x()+2*pin1->connectr()->size().width(),pin1->connectr()->pos().y()))),points[0]->inH, scene());
}
}
}
void Whire::DotDot(Whire* w)
{
enum doupin
{
pin1_pin1,
pin1_pin2,
pin2_pin1,
pin2_pin2
};
QPointF p2, wp2, doublepin;
doupin dpin;
if (pin1 == w->pin1)
{
dpin = pin1_pin1;
p2 = this->pin2->whirepos(pin1);
wp2 = w->pin2->whirepos(w->pin1);
doublepin = pin1->whirepos(pin2);
points.first()->setPos(pin1->whirepos(pin2));
points[1]->setY(points.first()->y());
w->points.first()->setPos(points.first()->pos());
w->points[1]->setY(w->points.first()->y());
}
if (pin1 == w->pin2)
{
dpin = pin1_pin2;
p2 = this->pin2->whirepos(pin1);
wp2 = w->pin1->whirepos(w->pin2);
doublepin = pin1->whirepos(pin2);
points.first()->setPos(pin1->whirepos(pin2));
points[1]->setY(points.first()->y());
w->points.last()->setPos(points.first()->pos());
w->points[w->points.size()-2]->setY(w->points.last()->y());
}
if (pin2 == w->pin1)
{
dpin = pin2_pin1;
p2 = this->pin1->whirepos(pin2);
wp2 = w->pin2->whirepos(w->pin1);
doublepin = pin2->whirepos(pin1);;
points.last()->setPos(pin2->whirepos(pin1));
points[points.size()-2]->setY(points.last()->y());
w->points.first()->setPos(points.first()->pos());
w->points[1]->setY(w->points.first()->y());
}
if (pin2 == w->pin2)
{
dpin = pin2_pin2;
p2 = this->pin1->whirepos(pin2);
wp2 = w->pin1->whirepos(w->pin2);
doublepin = pin2->whirepos(pin1);
points.last()->setPos(pin2->whirepos(pin1));
points[points.size() - 2]->setY(points.last()->y());
w->points.last()->setPos(points.first()->pos());
w->points[w->points.size() - 2]->setY(w->points.last()->y());
}
w->scene()->update();
return;
}
void Whire::collisionfix(Whire *w)
{
for (int j = 0; j < w->points.count() - 1; j++)
{
for (int i = 0; i < points.count() - 1; i++)
{
if (true);
{
if (points[i]->inH)
{
if (w->points[j]->inH)
{
if (abs(points[i]->pos().x() - w->points[j]->pos().x()) <= 40)
{
if (points[i - 1]->pos().x() <= points[i]->pos().x())
{
if (w->points[j - 1]->pos().x() <= w->points[j]->pos().x())
{
if (abs(points[i]->pos().y() - points[i + 1]->pos().y()) < abs(w->points[j]->pos().y() - w->points[j + 1]->pos().y()))
{
points[i]->setPos(points[i]->pos().x() - 35, points[i]->pos().y());
w->points[j]->setPos(w->points[j]->pos().x() + 25, w->points[j]->pos().y());
points[i + 1]->setPos(points[i + 1]->pos().x() - 35, points[i + 1]->pos().y());
w->points[j + 1]->setPos(w->points[j + 1]->pos().x() + 25, w->points[j + 1]->pos().y());
w->Update();
}
else
{
points[i]->setPos(points[i]->pos().x() + 35, points[i]->pos().y());
w->points[j]->setPos(w->points[j]->pos().x() - 25, w->points[j]->pos().y());
points[i + 1]->setPos(points[i + 1]->pos().x() + 35, points[i + 1]->pos().y());
w->points[j + 1]->setPos(w->points[j + 1]->pos().x() - 25, w->points[j + 1]->pos().y());
//w->Update();
}
}
else
{
points[i]->setPos(points[i]->pos().x() - 35, points[i]->pos().y());
w->points[j]->setPos(w->points[j]->pos().x() + 25, w->points[j]->pos().y());
points[i + 1]->setPos(points[i + 1]->pos().x() - 35, points[i + 1]->pos().y());
w->points[j + 1]->setPos(w->points[j + 1]->pos().x() + 25, w->points[j + 1]->pos().y());
w->Update();
}
}
else
{
if (w->points[j - 1]->pos().x() >= w->points[j]->pos().x())
{
if (abs(points[i]->pos().y() - points[i + 1]->pos().y()) < abs(w->points[j]->pos().y() - w->points[j + 1]->pos().y()))
{
points[i]->setPos(points[i]->pos().x() - 35, points[i]->pos().y());
w->points[j]->setPos(w->points[j]->pos().x() + 25, w->points[j]->pos().y());
points[i + 1]->setPos(points[i + 1]->pos().x() - 35, points[i + 1]->pos().y());
w->points[j + 1]->setPos(w->points[j + 1]->pos().x() + 25, w->points[j + 1]->pos().y());
w->Update();
}
else
{
points[i]->setPos(points[i]->pos().x() + 35, points[i]->pos().y());
w->points[j]->setPos(w->points[j]->pos().x() - 25, w->points[j]->pos().y());
points[i + 1]->setPos(points[i + 1]->pos().x() + 35, points[i + 1]->pos().y());
w->points[j + 1]->setPos(w->points[j + 1]->pos().x() - 25, w->points[j + 1]->pos().y());
//w->Update();
}
}
else
{
points[i]->setPos(points[i]->pos().x() - 35, points[i]->pos().y());
w->points[j]->setPos(w->points[j]->pos().x() + 25, w->points[j]->pos().y());
points[i + 1]->setPos(points[i + 1]->pos().x() - 35, points[i + 1]->pos().y());
w->points[j + 1]->setPos(w->points[j + 1]->pos().x() + 25, w->points[j + 1]->pos().y());
w->Update();
}
}
}
}
else
{
//Bridge;
}
}
else
{
if (!w->points[j]->inH)
{
if (abs(points[i]->pos().y() - w->points[j]->pos().y()) <= 40)
{
if (points[i]->pos().y() > w->points[j]->pos().y())
{
points[i]->setPos(points[i]->pos().x(), points[i]->pos().y() + 35);
w->points[j]->setPos(w->points[j]->pos().x(), w->points[j]->pos().y() - 25);
points[i + 1]->setPos(points[i + 1]->pos().x(), points[i + 1]->pos().y() + 35);
w->points[j + 1]->setPos(w->points[j + 1]->pos().x(), w->points[j + 1]->pos().y() - 25);
}
else
{
points[i]->setPos(points[i]->pos().x(), points[i]->pos().y() - 25);
w->points[j]->setPos(w->points[j]->pos().x(), w->points[j]->pos().y() + 35);
points[i + 1]->setPos(points[i + 1]->pos().x(), points[i + 1]->pos().y() - 25);
w->points[j + 1]->setPos(w->points[j + 1]->pos().x(), w->points[j + 1]->pos().y() + 35);
}
}
}
else
{
//Bridge;
}
}
}
}
}
w->pin1->addDot(true);
w->pin2->addDot(true);
}
void Whire::collisionfix(connector *cn)
{
}
void Whire::colChange(QColor co)
{
col=co;
pin1->ColorChange(col);
pin2->ColorChange(col);
}
QColor Whire::color()
{
return col;
}
QPointF Whire::NeartoPindot(pin *p,bool bl)
{
if(pin1==p)
{
return points[1]->pos();
}
else if(pin2==p)
{
return points[points.count() - 2]->pos();
}
}
QPointF Whire::NeartoPin(pin *p)
{
if(pin1==p)
{
return points[1]->pos();
}
else if(pin2==p)
{
return points[points.count()-2]->pos();
}
}
Whire::~Whire()
{
pin1->removeW(this);
pin2->removeW(this);
if(dynamic_cast<customviewMap*>(gview->view)!=nullptr)
{
dynamic_cast<customviewMap*>(gview->view)->Remove(this);
}
}
void Whire::drawLine(Dot* pos1, Dot* pos2, Dot* pos3, int &Radius2, QPainter* painter,bool last)
{
painter->setPen(QPen(col,8));
int Nrad;
if(!pos2->isVisible())
{
if(pos2->inH)
{
Nrad=qMin((double)Radius2,abs(pos2->pos().y()-pos3->pos().y())/10);
if (Nrad==0)
Nrad=abs(pos2->pos().y()-pos3->pos().y())/10;
if(pos1->pos().x()<=pos2->pos().x())
{
if(pos2->pos().y()<=pos3->pos().y())
{
painter->drawLine(pos1->pos().x()+Radius2,pos1->pos().y(),pos2->pos().x()-Nrad,pos2->pos().y());
painter->drawArc(pos2->pos().x()-2*Nrad,pos2->pos().y(),2*Nrad,2*Nrad,0*16,90*16);
}
else
{
painter->drawLine(pos1->pos().x()+Radius2,pos1->pos().y(),pos2->pos().x()-Nrad,pos2->pos().y());
painter->drawArc(pos2->pos().x()-2*Nrad,pos2->pos().y()-2*Nrad,2*Nrad,2*Nrad,270*16,90*16);
}
}
else
{
if(pos2->pos().y()<=pos3->pos().y())
{
painter->drawLine(pos1->pos().x()-Radius2,pos1->pos().y(),pos2->pos().x()+Nrad,pos2->pos().y());
painter->drawArc(pos2->pos().x(),pos2->pos().y(),2*Nrad,2*Nrad,90*16,90*16);
}
else
{
painter->drawLine(pos1->pos().x()-Radius2,pos1->pos().y(),pos2->pos().x()+Nrad,pos2->pos().y());
painter->drawArc(pos2->pos().x(),pos2->pos().y()-2*Nrad,2*Nrad,2*Nrad,180*16,90*16);
}
}
}
else
{
Nrad=qMin((double)Radius2,abs(pos2->pos().x()-pos3->pos().x())/10);
if(pos1->pos().y()<=pos2->pos().y())
{
if(pos2->pos().x()<=pos3->pos().x())
{
painter->drawLine(pos1->pos().x(),pos1->pos().y()+Radius2,pos2->pos().x(),pos2->pos().y()-Nrad);
painter->drawArc(pos2->pos().x(),pos2->pos().y()-2*Nrad,2*Nrad,2*Nrad,180*16,90*16);
if(last)
painter->drawLine(pos2->pos().x()+Nrad,pos2->pos().y(),pos3->pos().x(),pos3->pos().y());
}
else
{
painter->drawLine(pos1->pos().x(),pos1->pos().y()+Radius2,pos2->pos().x(),pos2->pos().y()-Nrad);
painter->drawArc(pos2->pos().x()-2*Nrad,pos2->pos().y()-2*Nrad,2*Nrad,2*Nrad,270*16,90*16);
if(last)
painter->drawLine(pos2->pos().x()-Nrad,pos2->pos().y(),pos3->pos().x(),pos3->pos().y());
}
}
else
{
if(pos2->pos().x()<=pos3->pos().x())
{
painter->drawLine(pos1->pos().x(),pos1->pos().y()-Radius2,pos2->pos().x(),pos2->pos().y()+Nrad);
painter->drawArc(pos2->pos().x(),pos2->pos().y(),2*Nrad,2*Nrad,90*16,90*16);
if(last)
painter->drawLine(pos2->pos().x()+Nrad,pos2->pos().y(),pos3->pos().x(),pos3->pos().y());
}
else
{
painter->drawLine(pos1->pos().x(),pos1->pos().y()-Radius2,pos2->pos().x(),pos2->pos().y()+Nrad);
painter->drawArc(pos2->pos().x()-2*Nrad,pos2->pos().y(),2*Nrad,2*Nrad,0*16,90*16);
if(last)
painter->drawLine(pos2->pos().x()-Nrad,pos2->pos().y(),pos3->pos().x(),pos3->pos().y());
}
}
}
Radius2=Nrad;
}
else
{
painter->drawLine(pos1->pos(),pos2->pos());
painter->drawLine(pos1->pos(),pos2->pos());
painter->drawEllipse(pos2->pos(),5,5);
}
}
void Whire::drawLine(Dot* p1, Dot* p2, QPainter* p)
{
p->setPen(QPen(col, 8));
p->drawLine(p1->pos(), p2->pos());
}
QRectF Whire::boundingRect() const
{
QRectF rect= shape().boundingRect();
rect.setRect(rect.x() - 10, rect.y() - 10, rect.width() + 10, rect.height() + 10);
return rect;
}
QPainterPath Whire::shape() const
{
QPainterPath p=QPainterPath();
int x,y,w,h;
for(int i=0;i<points.count()-1;i++)
{
x=qMin(points[i]->pos().x(),points[i+1]->pos().x());
x-=5;
y=qMin(points[i]->pos().y(),points[i+1]->pos().y());
y-=5;
w=abs(points[i]->pos().x()-points[i+1]->pos().x());
w+=10;
h=abs(points[i]->pos().y()-points[i+1]->pos().y());
h+=10;
p.addRect(x,y,w,h);
}
return p;
}
void Whire::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
if(e->modifiers()&Qt::SHIFT)
{
delete this;
}
else
{
if(col!=Qt::red)
col=Qt::red;
else
col=Qt::darkGray;
Update();
}
}
I'm currently programming an alarm clock with a 4x4 keypad, i2c 20x4 Lcd, DS3231 and buzzer.
Everything works, but I have problems with implementing the menu to change alarm times and other settings.
Sadly I'm under time pressure so this is very important to me. Any help is acknowledged.
The menu shows up and when I press a key it returns to the normal screen showing the clock. I tried debugging with Serial and it breaks off here (a schematic ) :
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
// A Serial Print here still works but after a key is pressed it returns to the clock.
}
if (keypressm == "1") {
menuSetAlarm();
} else if (keypressm == "2") {
menuSetLanguage();
} else if (keypressm == "3") {
menuOtherSettings();
} else if (keypressm == "9") {
lcd.clear();
loop();
}
lcd.clear();
}
Here's the whole code (without EEPROM stuff)
#include <TimedAction.h>
#include <Wire.h> // I2C Adress
#include <LiquidCrystal_I2C.h> // Library for I2C LCD Displays
#include <Keypad.h> // Keypad Libary
#include "RTClib.h" // Library for DS3231 RTC
//Hier wird die größe des Keypads definiert
const byte COLS = 4; //4 Spalten
const byte ROWS = 4; //4 Zeilen
//Die Ziffern und Zeichen des Keypads werden eingegeben:
char hexaKeys[ROWS][COLS] = {
{'1', '4', '7', '*'},
{'2', '5', '8', '0'},
{'3', '6', '9', '#'},
{'A', 'B', 'C', 'D'}
};
RTC_DS3231 rtc; // declare RTC
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // declare LCD module (I2C adress, RS, E, D4, D5, D6, D7, backlightpin, polarity)
DateTime now;
byte colPins[COLS] = {2, 3, 4, 5}; //Definition der Pins für die 4 Spalten
byte rowPins[ROWS] = {6, 7, 8, 9}; //Definition der Pins für die 4 Zeilen
char key; //Taste ist die Variable für die jeweils gedrückte Taste.
char keypressx; //Taste um alarm abzubrechen
char keypressm; //menukeyw
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld" angesprochen werden
Keypad Tastenfeld2 = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld2" angesprochen werden
int menupos = -1;
boolean currentStateBacklight = true;
boolean scheduledBacklight = false;
int bs_ontime;
int bs_offtime;
const int alarm_pin = 10;
boolean endedAlarm = false;
int endedAlarmTime;
boolean alarm1_active = false;
boolean alarm2_active = false;
String alarm1_hour;
String alarm1_minute;
String alarm1_second;
String alarm1Days[] = {};
String alarm2_hour;
String alarm2_minute;
String alarm2_second;
String alarm2Days[] = {};
char daysOfTheWeek[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
void setup() {
Serial.begin(250000);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.setBacklight(HIGH);
lcd.begin(20, 4); // set up the LCD's number of columns and rows
Wire.begin();
}
void loop() {
key = NO_KEY;
keypressx = NO_KEY;
keypressm = NO_KEY;
while (key == NO_KEY) {
key = Tastenfeld.getKey();
DateTime now = rtc.now();
if (endedAlarm) {
if ((millis() - endedAlarmTime) > 60000) {
endedAlarm = false;
endedAlarmTime = 0;
}
}
String nowhour = correctedValue(now.hour());
String nowmin = correctedValue(now.minute());
String nowsec = correctedValue(now.second());
String nowday = daysOfTheWeek[now.dayOfTheWeek()];
if (alarm1_active && nowhour == alarm1_hour && nowmin == alarm1_minute && !endedAlarm && arrayIncludeElement(alarm1Days, nowday)) { // && correctedValue(now.second()) == alarm1_second) {
lcd.clear();
lcd.setCursor(1, 10);
lcd.print("Get up !!!"); //Message to show when the alarm is ringing
int z = 0;
int x = 1000;
int y = 430;
while (keypressx == NO_KEY) {
tone(alarm_pin, 2870);
keypressx = Tastenfeld.getKey();
delay(y);
noTone(alarm_pin);
keypressx = Tastenfeld.getKey();
delay(x);
x = x * 0.95999;
y = y * 0.97999;
keypressx = Tastenfeld.getKey();
}
endedAlarm = true;
endedAlarmTime = millis();
lcd.clear();
}
if (alarm2_active && nowhour == alarm2_hour && nowmin == alarm2_minute && !endedAlarm && arrayIncludeElement(alarm2Days, nowday)) {
lcd.clear();
lcd.setCursor(1, 10);
lcd.print("Get up !!!"); //Message to show when the alarm is ringing
int z = 0;
int x = 1000;
int y = 430;
while (keypressx == NO_KEY) {
tone(alarm_pin, 2870);
keypressx = Tastenfeld.getKey();
delay(y);
noTone(alarm_pin);
keypressx = Tastenfeld.getKey();
delay(x);
x = x * 0.95999;
y = y * 0.97999;
keypressx = Tastenfeld.getKey();
}
endedAlarm = true;
endedAlarmTime = millis();
lcd.clear();
}
keypressx = NO_KEY;
noTone(alarm_pin);
now = rtc.now();
lcd.setCursor(0, 0); //Text soll beim ersten Zeichen in der ersten Reihe beginnen..
lcd.print(""); //In der ersten Zeile soll der Text „Test Zeile 1“ angezeigt werden
lcd.setCursor(18, 0);
if (alarm1_active == true) {
lcd.print("A1");
}
lcd.setCursor(6, 1); //Genauso geht es bei den weiteren drei Zeilen weiter
lcd.print(correctedValue(now.hour()) + ":" + correctedValue(now.minute()) + ":" + correctedValue(now.second()));
lcd.setCursor(18, 1);
if (alarm2_active == true) {
lcd.print("A2");
}
lcd.setCursor(7, 2);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(5, 3);
lcd.print(correctedValue(now.day()) + "." + correctedValue(now.month()) + "." + String(now.year()));
delay(100);
}
Serial.println(key);
if (key == '1') {
if (alarm1_active) {
alarm1_active = false;
lcd.clear();
} else {
alarm1_active = true;
}
}
if (key == '2') {
if (alarm2_active) {
alarm2_active = false;
lcd.clear();
} else {
alarm2_active = true;
}
}
if (key == '*') {
menu(0);
}
}
boolean arrayIncludeElement(String array[], String element) {
for (int i = 0; i < 7; i++) {
if (array[i] == element) {
return true;
}
}
return false;
}
String correctedValue(char x) {
int y;
String z;
y = int(x);
if (y < 10) {
z = String(y);
z = "0" + z;
return z;
} else {
return String(y);
}
}
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
key = "*";
}
if (keypressm == "1") {
//menupos = 1;
menuSetAlarm();
//menupos = 0;
} else if (keypressm == "2") {
//menupos = 2;
menuSetLanguage();
//menupos = 0;
} else if (keypressm == "3") {
//menupos = 3;
menuOtherSettings();
//menupos = 0;
} else if (keypressm == "9") {
lcd.clear();
loop();
//menupos = -1;
}
lcd.clear();
}
void menuSetAlarm() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarm 1");
lcd.setCursor(0, 1);
lcd.print("2. Alarm 2");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
} else if (keypressm == "2") {
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
menu(1);
lcd.clear();
}
void menuSetLanguage() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Deutsch");
lcd.setCursor(0, 1);
lcd.print("2. English");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
changeLanguage("en");
} else if (keypressm == "2") {
changeLanguage("de");
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
lcd.clear();
menu(2);
}
void menuOtherSettings() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Permanent " + !currentStateBacklight);
lcd.setCursor(0, 1);
lcd.print("2. Geplant");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
currentStateBacklight = !currentStateBacklight;
if (currentStateBacklight) {
lcd.setBacklight(HIGH);
} else {
lcd.setBacklight(LOW);
}
} else if (keypressm == "2") {
scheduledBacklight = true;
int x;
int y;
// set on hour
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print("XX");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (keypressm != "#" && keypressm != "*") {
if (keypressm == "0" || keypressm == "1" || keypressm == "2") {
if (keypressm = "0") {
y = 0;
} else if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
}
x = y;
}
menu(3);
}
// 2nd digit
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print(x + "X");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (x == 2) {
if (keypressm == "0" || keypressm == "1" || keypressm == "2" || keypressm == "3" ) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
}
x = (x * 10) + y;
bs_ontime = x;
} else if (keypressm == "4") {
bs_ontime = 0;
}
}
if (x == 1) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_ontime = x;
}
if (x == 0) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_ontime = x;
}
// set on hour
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print("XX");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (keypressm != "#" && keypressm != "*") {
if (keypressm == "0" || keypressm == "1" || keypressm == "2") {
if (keypressm = "0") {
y = 0;
} else if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
}
x = y;
}
menu(3);
}
// 2nd digit
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print(x + "X");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (x == 2) {
if (keypressm == "0" || keypressm == "1" || keypressm == "2" || keypressm == "3" ) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
}
x = (x * 10) + y;
bs_offtime = x;
} else if (keypressm == "4") {
bs_offtime = 0;
}
}
if (x == 1) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_offtime = x;
}
if (x == 0) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_offtime = x;
lcd.clear();
}
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
lcd.clear();
menupos = 1;
}
I think, the problem is loop();. Test the return; method. Example:
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
// A Serial Print here still works but after a key is pressed it returns to the clock.
}
if (keypressm == "1") {
menuSetAlarm();
} else if (keypressm == "2") {
menuSetLanguage();
} else if (keypressm == "3") {
menuOtherSettings();
} else if (keypressm == "9") {
lcd.clear();
return; // !!
}
lcd.clear();
}
Good luck!
I am trying to code following recursion problem. Every time I run the test case it gives me out of bound error on the else if line. How do I fix the out of bound error. under the code is the test case.
static boolean allEqual(int[] a, int start, int end) {
if( a[start + 1]>=a.length){return false;}
if (start == end && a[start] == a[end]) {
return true;
}
///recursive
else if (a[start] == a[start + 1]) // Error over here
{
return allEqual(a, start + 1, end);
}
// else if (start == end && a[start] == a[end]) {
// return true;
else {
return false;
}
}
// Following is the test case
Random rng = new Random(SEED);
for(int i = 0; i < RUNS; i++) {
int[] a = new int[i];
int v = rng.nextInt();
for(int j = 0; j < i; j++) {
a[j] = v;
}
assertTrue(rp.allEqual(a, 0, i-1));
for(int j = 1; j < i; j++) {
assertTrue(rp.allEqual(a, j, i-1));
assertTrue(rp.allEqual(a, 0, j));
a[j] = a[j] - 1;
assertTrue(rp.allEqual(a, 0, j-1));
assertTrue(rp.allEqual(a, j + 1, i-1));
assertFalse(rp.allEqual(a, 0, i-1));
a[j] = a[j] + 2;
assertTrue(rp.allEqual(a, 0, j-1));
assertTrue(rp.allEqual(a, j + 1, i-1));
assertFalse(rp.allEqual(a, 0, i-1));
a[j] = a[j] - 1;
}
}
I'm having a dilemma with this code and have no clue what to do. I'm pretty new to processing. This is a project from this link...
http://blog.makezine.com/2012/08/10/build-a-touchless-3d-tracking-interface-with-everyday-materials/
any help is massively appreciated... Thanks in advance
import processing.serial.*;
import processing.opengl.*;
Serial serial;
int serialPort = 1;
int sen = 3; // sensors
int div = 3; // board sub divisions
Normalize n[] = new Normalize[sen];
MomentumAverage cama[] = new MomentumAverage[sen];
MomentumAverage axyz[] = new MomentumAverage[sen];
float[] nxyz = new float[sen];
int[] ixyz = new int[sen];
float w = 256; // board size
boolean[] flip = {
false, true, false};
int player = 0;
boolean moves[][][][];
PFont font;
void setup() {
size(800, 600, P3D);
frameRate(25);
font = loadFont("TrebuchetMS-Italic-20.vlw");
textFont(font);
textMode(SCREEN);
println(Serial.list());
serial = new Serial(this, Serial.list()[serialPort], 115200);
for(int i = 0; i < sen; i++) {
n[i] = new Normalize();
cama[i] = new MomentumAverage(.01);
axyz[i] = new MomentumAverage(.15);
}
reset();
}
void draw() {
updateSerial();
drawBoard();
}
void updateSerial() {
String cur = serial.readStringUntil('\n');
if(cur != null) {
String[] parts = split(cur, " ");
if(parts.length == sensors) {
float[] xyz = new float[sen];
for(int i = 0; i < sen; i++)
xyz[i] = float(parts[i]);
if(mousePressed && mouseButton == LEFT)
for(int i = 0; i < sen; i++)
n[i].note(xyz[i]);
nxyz = new float[sen];
for(int i = 0; i < sen; i++) {
float raw = n[i].choose(xyz[i]);
nxyz[i] = flip[i] ? 1 - raw : raw;
cama[i].note(nxyz[i]);
axyz[i].note(nxyz[i]);
ixyz[i] = getPosition(axyz[i].avg);
}
}
}
}
float cutoff = .2;
int getPosition(float x) {
if(div == 3) {
if(x < cutoff)
return 0;
if(x < 1 - cutoff)
return 1;
else
return 2;
}
else {
return x == 1 ? div - 1 : (int) x * div;
}
}
void drawBoard() {
background(255);
float h = w / 2;
camera(
h + (cama[0].avg - cama[2].avg) * h,
h + (cama[1].avg - 1) * height / 2,
w * 2,
h, h, h,
0, 1, 0);
pushMatrix();
noStroke();
fill(0, 40);
translate(w/2, w/2, w/2);
rotateY(-HALF_PI/2);
box(w);
popMatrix();
float sw = w / div;
translate(h, sw / 2, 0);
rotateY(-HALF_PI/2);
pushMatrix();
float sd = sw * (div - 1);
translate(
axyz[0].avg * sd,
axyz[1].avg * sd,
axyz[2].avg * sd);
fill(255, 160, 0);
noStroke();
sphere(18);
popMatrix();
for(int z = 0; z < div; z++) {
for(int y = 0; y < div; y++) {
for(int x = 0; x < div; x++) {
pushMatrix();
translate(x * sw, y * sw, z * sw);
noStroke();
if(moves[0][x][y][z])
fill(255, 0, 0, 200);
else if(moves[1][x][y][z])
fill(0, 0, 255, 200);
else if(
x == ixyz[0] &&
y == ixyz[1] &&
z == ixyz[2])
if(player == 0)
fill(255, 0, 0, 200);
else
fill(0, 0, 255, 200);
else
fill(0, 100);
box(sw / 3);
popMatrix();
}
}
}
fill(0);
if(mousePressed && mouseButton == LEFT)
msg("defining boundaries");
}
void keyPressed() {
if(key == TAB) {
moves[player][ixyz[0]][ixyz[1]][ixyz[2]] = true;
player = player == 0 ? 1 : 0;
}
}
void mousePressed() {
if(mouseButton == RIGHT)
reset();
}
void reset() {
moves = new boolean[2][div][div][div];
for(int i = 0; i < sen; i++) {
n[i].reset();
cama[i].reset();
axyz[i].reset();
}
}
void msg(String msg) {
text(msg, 10, height - 10);
}
You are missing a class, in fact, more than one. Go back to the github and download, or copy and paste, all three codes, placing each one in a new tab named same name of the class (well this is not required, but is a good practice). The TicTacToe3D.pde is the main code. To make a new tab choose "new tab" from the arrow menu in Processing IDE (just below the standard button at the right). The code should run. WIll need an Arduino though to really get it working.