How can I count another var ?

Hello, I´m struggling on something and I need help !

I am sitting on an Arduino nano with Oled Display 1.3" RTC DS3231, everything works so far ! It is just another Geigercounter project of mine. :slight_smile:
The RTC is used as timer in that project and sets the variable "x" to 1 and 0 every second.
I just want to make a simple addition/counting but it doesn´t work like I want it to.

I just want to increase another variable for 1, when x=1. For exaple variable "m10s".
But in the moment x=1 the other variable, m10s just keeps adding 1 to it until x=0. I just want to increase the var. "m10s" just for 1. Then it has to wait untill x=0 and becomes 1 again. I just want to count x how many times it becomes 1, that´s all !

How can I do it ? why does it keep adding ? This is the part of code that doesn´t work as I thought it would. I tried it with IF/then cases and I just got the same problem !

switch (x) { //x=1 for one second / x=0 for one second
case 0:
m10s = m10s +1; // this is the spott where it keeps just adding 1.
break;
case 1:
m10smem = m10smem +1; // this is the spott where it keeps just adding 1.
break;
}

How can I say the compiler just add one and then stop ! Wait for the next state X=1.
I think, it is clear if I say somthing like m10s = m10s +1; ,that I only want to add one to it in that case.
Why does it work on my Atari 800XL in BASIC ? The Atari just adds on if I programm it like this. Why doesn´t die ArduinoIDE do it ??? Sucks ? I couldn´t find any other help in the internet about it !

I could need a command like : wait until x=0; :slight_smile:

Can someone help ???
Cheers ! :slight_smile:

Hello

Your switch is probably directly inside loop, but it should be executed only when value of x has changed.

Use another variable previous_x to remember the previous value of x. Then :

if ( previous_x == 0 && x == 1 ) // if x changed from 0 to 1
{
    ...
}

:face_with_spiral_eyes:

Eyes glossing over.



In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.


Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.

The concept you need is called "state change detection", or edge detection.

See Files/Examples/02.Digital/StateChangeDetection on offer in the IDE.

google

arduino state change detection

for the Arduino article on the matter.

a7

Please post the entire program or a Minimal, Reproducible Example that shows the problem. Snippets leave out important information.

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.

Wow ! That went quick ! Thanks ! I´ll try it and come back later !

That went qick ! Thanks ! I will take a look ! Will be back, soon !
Thanks again !

Hello and thanks, this is the code, still under construcktion. But it will run like that on a arduino nano with Oled display 128x64 Ic2. Then You can see the problem I´m struggling with. :slight_smile:
Cheers !

#include <I2C_RTC.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

static DS3231 RTC;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define BAT0_HEIGHT 8
#define BAT0_WIDTH 16
static const unsigned char PROGMEM bat0_bmp[] =
{ 0b00111111, 0b11111111,
0b00100000, 0b00000001,
0b01100000, 0b00000001,
0b11100000, 0b00000001,
0b11100000, 0b00000001,
0b01100000, 0b00000001,
0b00100000, 0b00000001,
0b00111111, 0b11111111
};

#define BAT25_HEIGHT 8
#define BAT25_WIDTH 16
static const unsigned char PROGMEM bat25_bmp[] =
{ 0b00111111, 0b11111111,
0b00100000, 0b00001111,
0b01100000, 0b00001111,
0b11100000, 0b00001111,
0b11100000, 0b00001111,
0b01100000, 0b00001111,
0b00100000, 0b00001111,
0b00111111, 0b11111111
};
#define BAT50_HEIGHT 8
#define BAT50_WIDTH 16
static const unsigned char PROGMEM bat50_bmp[] =
{ 0b00111111, 0b11111111,
0b00100000, 0b01111111,
0b01100000, 0b01111111,
0b11100000, 0b01111111,
0b11100000, 0b01111111,
0b01100000, 0b01111111,
0b00100000, 0b01111111,
0b00111111, 0b11111111
};
#define BAT75_HEIGHT 8
#define BAT75_WIDTH 16
static const unsigned char PROGMEM bat75_bmp[] =
{ 0b00111111, 0b11111111,
0b00100011, 0b11111111,
0b01100011, 0b11111111,
0b11100011, 0b11111111,
0b11100011, 0b11111111,
0b01100011, 0b11111111,
0b00100011, 0b11111111,
0b00111111, 0b11111111
};
#define BAT100_HEIGHT 8
#define BAT100_WIDTH 16
static const unsigned char PROGMEM bat100_bmp[] =
{ 0b00111111, 0b11111111,
0b00111111, 0b11111111,
0b01111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111111,
0b00111111, 0b11111111,
0b00111111, 0b11111111
};

#define BAT0_HEIGHT 32
#define BAT0_WIDTH 40
static const unsigned char PROGMEM radioaktiv_bmp[] =
{
0b00000000, 0b01000000, 0b00000000, 0b00000010, 0b00000000,
0b00000000, 0b11100000, 0b00000000, 0b00000111, 0b00000000,
0b00000001, 0b11110000, 0b00000000, 0b00001111, 0b10000000,
0b00000011, 0b11111000, 0b00000000, 0b00011111, 0b11000000,
0b00000111, 0b11111100, 0b00000000, 0b00111111, 0b11100000,
0b00000111, 0b11111110, 0b00000000, 0b01111111, 0b11100000,
0b00001111, 0b11111111, 0b00000000, 0b11111111, 0b11110000,
0b00001111, 0b11111111, 0b10000001, 0b11111111, 0b11110000,
0b00011111, 0b11111111, 0b10000001, 0b11111111, 0b11111000,
0b00011111, 0b11111111, 0b00000000, 0b11111111, 0b11111000,
0b00011111, 0b11111110, 0b00011000, 0b01111111, 0b11111000,
0b00111111, 0b11111110, 0b01111110, 0b01111111, 0b11111100,
0b00000111, 0b11111100, 0b01111110, 0b00111111, 0b11100000,
0b00000000, 0b11111100, 0b11111111, 0b00111111, 0b00000000,
0b00000000, 0b00001100, 0b11111111, 0b00110000, 0b00000000,
0b00000000, 0b00000000, 0b01111110, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b01111110, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00011000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b11000011, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b11111111, 0b00000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000111, 0b11111111, 0b11100000, 0b00000000,
0b00000000, 0b00000111, 0b11111111, 0b11100000, 0b00000000,
0b00000000, 0b00000000, 0b01111110, 0b00000000, 0b00000000
};
const int button1 = 10;
const int button2 = 11;
const int button3 = 12;
const int led = 13;
int hour;

int poti1 = A0;
int Analog = 0;
int prozent = 0;
int x = 0;
int m10s = 0;
int m10smem = 0;

void setup() {
Serial.begin(57600);
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(button3,INPUT);
pinMode(led,OUTPUT);

RTC.begin();

//RTC.setHourMode(CLOCK_H12);
// RTC.setHourMode(CLOCK_H24);

/* This will set the clock: uncomment if done !
RTC.setDay(29);
RTC.setMonth(9);
RTC.setYear(2023);

RTC.setHours(23);
RTC.setMinutes(55);
RTC.setSeconds(00);

RTC.setWeek(39);

//RTC.setDate(23,09,29);
//RTC.setTime(22,42,20);/
*/

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.drawBitmap(40, 14, radioaktiv_bmp, 40, 32, WHITE);
display.display();
delay(2000);

//Timer als counter:
TCCR1A = 0; // Init Timer1A
TCCR1B = 0; // Init Timer1B
TCCR1B |= B00000111; // External Clock on T1 PinB5 (RISING)
TCNT1 = 0;
}

void loop() {
display.clearDisplay();
char count[8];
sprintf(count,"%02i",TCNT1);
// Analogwert wurde formatiert ! display.println(analog);
Analog = analogRead(poti1);

//Gerade/ungerade sekunden erkennen:
if(RTC.getSeconds() % 2 == 0) {
x = 1;
}else {
x = 0;
}
switch (x) {
case 0:
m10s = m10s +1;
break;
case 1:
m10smem = m10smem +1;
break;
}

if (digitalRead(button1) == HIGH) {
clockset();
}
// Format time for display:
char stunden[8];
sprintf(stunden,"%02i",RTC.getHours());

char minuten[8];
sprintf(minuten,"%02i",RTC.getMinutes());

char sekunden[8];
sprintf(sekunden,"%02i",RTC.getSeconds());

// make a string for the display : display.println(zeit) !
String zeit = String(stunden) + ":" + String(minuten) + ":" + String(sekunden);

display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);

display.setCursor(0, 0);
display.println(zeit);

 display.setCursor(0, 50);
 display.println(x);

      display.setCursor(30, 50);
      display.println(m10s);
      display.setCursor(70, 50);
      display.println(m10smem);

  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(50,30);
  display.println(count);

batterie();

display.display(); // Show initial text

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (digitalRead(button1) == HIGH) {
// turn LED on:
digitalWrite(led, HIGH);

} else {
// turn LED off:
digitalWrite(led, LOW);

}

}
// void to set the RTC by buttons
void clockset() {
hour = RTC.getHours();
hour = hour +1;
if (hour > 23) {
hour = 0;
}
RTC.setHours(hour);

delay(250);
}
//void vor battery status !
void batterie() {
Analog = analogRead(poti1);

if (Analog < 254) {
display.drawBitmap(112, 0, bat0_bmp, 16, 8, WHITE);
}
if (Analog > 249) {
display.drawBitmap(112, 0, bat25_bmp, 16, 8, WHITE);
}
if (Analog > 499) {
display.drawBitmap(112, 0, bat50_bmp, 16, 8, WHITE);
}
if (Analog > 749) {
display.drawBitmap(112, 0, bat75_bmp, 16, 8, WHITE);
}
if (Analog > 988) {
display.drawBitmap(112, 0, bat100_bmp, 16, 8, WHITE);
}

}

/*
switch (RTC.getWeek())
{
case 1:
Serial.print("SUN");
break;
case 2:
Serial.print("MON");
break;
case 3:
Serial.print("TUE");
break;
case 4:
Serial.print("WED");
break;
case 5:
Serial.print("THU");
break;
case 6:
Serial.print("FRI");
break;
case 7:
Serial.print("SAT");
break;
}
Serial.print(" ");
Serial.print(RTC.getDay());
Serial.print("-");
Serial.print(RTC.getMonth());
Serial.print("-");
Serial.print(RTC.getYear());

Serial.print(" ");

Serial.print(RTC.getHours());
Serial.print(":");
Serial.print(RTC.getMinutes());
Serial.print(":");
Serial.print(RTC.getSeconds());
Serial.print(" ");

if (RTC.getHourMode() == CLOCK_H12)
{
if(RTC.getMeridiem() == HOUR_AM)
Serial.println(" AM");
if (RTC.getMeridiem() == HOUR_PM)
Serial.println(" PM");
}
delay(10);
*/

I just tryed it, same problem ! Unfortunatelly, this isn´t working !
I found 3 ways now, to get the same result ! :slight_smile:

Please see paragraphs 2 and 3 of post #5. Post your code properly, so we may read it and comment on it.

Awesome !!! It works !
Thank you so much ! I was f***ing orund on it for days !
Finaly ! Thanks again ! You made my day !
Cheers ! :slight_smile:

How about posting your final code, then, so others may learn from your exploits? Oh, and give Alto a "solved" flag, while you're at it.

@magnetstein I don't get the solution "ticky", @alto777 does.
I get the "good advice, blissfully ignored" ticky. :man_shrugging:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.