How can I change the screen using the vibration sensor

hey i have a problem with my code. I would like my display to switch after vibration. I have two separate programs that work, but as soon as I want to combine them, I can't, any advice?

first:

  #include <Arduino.h>
  #include <Wire.h>

  //displej
  #include <Adafruit_GFX.h>
  #include <Adafruit_SH110X.h>
  #define i2c_Address 0x3c
  #define OLED_RESET -1
  #define SCREEN_WIDTH 128
  #define SCREEN_HEIGHT 64 
  #define OLED_RESET -1  
  Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

  //CO2
  int CO2Pin = 14;
  int CO2Puls = digitalPinToInterrupt(CO2Pin);
  int koncentrace;
  long HSTick=0, HETick=0;
  long HHodnoty=0, LHodnoty=0;
  byte a=0;
  double HHodnoty_ms;

  //BME280
  #include <Adafruit_BME280.h>
  #define BME_SCK 13
  #define BME_MISO 12
  #define BME_MOSI 11
  #define BME_CS 10
  #define SEALEVELPRESSURE_HPA (1013.25)
  Adafruit_BME280 bme; 
  int teplota;
  int tlak;
  int vlhkost;

void CO2Interrupt()
{
  if (digitalRead(CO2Pin)) {
    HSTick = micros();    // store the current micros() value
    if(2 == a){
      a = 4;
      if(HSTick > HETick) {
        LHodnoty = HSTick - HETick;
      }
    }else{
      a = 1;
    }
  } else {
    HETick = micros();    // store the current micros() value
    if(1 == a){
      a = 2;
      if(HETick > HSTick){
        HHodnoty = HETick - HSTick;
      }
    }
  }
}

void Oxid()
{
    if(a == 4){
    a = 1;

    HHodnoty_ms = (HHodnoty * 1000.0) / (LHodnoty + HHodnoty);

    if (HHodnoty_ms < 0.01){
      Serial.println("Chyba");
    }
    else if (HHodnoty_ms < 80.00){
      Serial.println("zahrivani");
    }
    else if (HHodnoty_ms < 998.00){
      koncentrace = (HHodnoty_ms - 2) * 5;
      //Print CO2 koncentrace
      Serial.print(koncentrace);
      Serial.println("ppm");
  
    }else{
      Serial.println("Za hranici mereni : 398~4980ppm");
    }
    Serial.println();
  }
}

void BME() {
  teplota = bme.readTemperature();
  tlak = bme.readPressure()/100;
  vlhkost = bme.readHumidity();
  if (vlhkost > 100) {
    vlhkost = 100;
  }

  Serial.print(teplota);
  Serial.println("C");
  Serial.print(vlhkost);
  Serial.println("%");
  Serial.print(tlak);
  Serial.println("hPa");
  Serial.println();
}

void setup() {
  Serial.begin(115200);

  display.begin(i2c_Address, true);
  display.display();
  delay(2000);
  display.clearDisplay();

  pinMode(CO2Pin, INPUT);
  attachInterrupt(CO2Puls, CO2Interrupt, CHANGE);

  bme.begin();
}


void loop() 
{
  Oxid();
  BME();

  display.setTextSize(1);
  display.clearDisplay();
  display.setTextColor(SH110X_WHITE);

  display.setCursor(11 , 8);
  display.print("Teplota: ");
  display.setCursor(66 , 8); 
  display.print(teplota);
  display.print(" ");
  display.print((char)247);                                     // symbol stupně
  display.println("C");

  display.setCursor(11 , 24);
  display.println("Tlak: ");
  display.setCursor(66 , 24);
  display.print(tlak);
  display.print(" hPa");

  display.setCursor(11 , 40);
  display.println("Vlhkost: ");
  display.setCursor(66 , 40);
  display.print(vlhkost);
  display.print(" %");

  display.setCursor(11 , 56);
  display.println("CO2: ");
  display.setCursor(66 , 56);
   if (HHodnoty_ms < 0.01){
      display.println("Chyba");
    }
    else if (HHodnoty_ms < 80.00){
      display.println("Zahrivani");
    }
    else if (HHodnoty_ms < 998.00){
      display.print(koncentrace);
      display.println("ppm");
  
    }
    else{
      display.println("Za hranici mereni : 398~4980ppm");
    }
  
  display.display();
  delay(1000);
}

second:

  #include <Arduino.h>
  #include <Wire.h>
  #include <Adafruit_GFX.h>
  #include <Adafruit_SH110X.h>

  #define i2c_Address 0x3c
  #define OLED_RESET -1
  #define SCREEN_WIDTH 128
  #define SCREEN_HEIGHT 64 
  #define OLED_RESET -1  

Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int vibPin=0;
int ledPin=2; 
int pocet;
int pred;

void setup()
{
  pinMode(vibPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);

  display.begin(i2c_Address, true);
  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);
  display.display();
  delay(2000);
  display.clearDisplay() ;
}

void vib()
{
  int vibState = digitalRead(vibPin);
  if (vibState == HIGH && pred ==0) {
    if (pocet<2) {
      pocet++;
    Serial.println(pocet);

    digitalWrite(ledPin, HIGH);
    delay(100);
    
    digitalWrite(ledPin, LOW);
    
    pred=1;

    }
    else {
      pocet=0;
    }
  }
  
  else if (vibState == LOW) {
    pred=0;
  }
}

void loop()
{ 
  
vib();
}

thank you

The best approach is to start with one of the working programs, and add new features to it, one at a time, testing as you go.

1 Like

Yea, I've tried, but I always get stuck on the same things. I've been stuck on this for a week now and I don't have any more ideas.

But thank you

Your "vib()" function in the second sketch can be wholly moved to the first sketch. Then, add the call "vib();" to the first sketch and run the first sketch. Tell us what happens? You will see errors, so show those, too.

1 Like

Post one of those attempts and explain what goes wrong. Then forum members can actually be helpful.

1 Like

It doesn't give any errors, but the vibration sensor doesn't do anything

the vibration sensor doesn't do anything

Probably a wiring or pin error. Along with the code, post a pic of a hand drawn wiring diagram, with pins, parts and connections clearly labeled.

This one is the most "successful"

  #include <Arduino.h>
  #include <Wire.h>
  #include <Adafruit_GFX.h>
  #include <Adafruit_SH110X.h>
  #include <Adafruit_Sensor.h>
  #include <Adafruit_BME280.h>

  #define i2c_Address 0x3c
  #define OLED_RESET -1
  #define SCREEN_WIDTH 128
  #define SCREEN_HEIGHT 64 
  #define OLED_RESET -1  

  Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

  int vibPin=0;
  int ledPin=2; 

  int vibState=0;
  int pocet=0;
  int pred=0; 

  #define BME_SCK 13
  #define BME_MISO 12
  #define BME_MOSI 11
  #define BME_CS 10
  #define SEALEVELPRESSURE_HPA (1013.25)


  Adafruit_BME280 bme; 

  int teplota = 0;
  int vlhkost = 0;
  int tlak = 0; 

  int CO2Sensor = 26;
  int CO2 = 0;

void setup()
{
  pinMode(vibPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  bme.begin();

  display.begin(i2c_Address, true);
  display.display();
  delay(2000);
  display.clearDisplay() ;

  float teplota = bme.readTemperature();
  float tlak = bme.readPressure();
  float vlhkost = bme.readHumidity();
}

void loop()
{ 
  
  int vibState = digitalRead(vibPin);

  if (vibState == HIGH && pred ==-1) {
    if (pocet<3) 
    {
      pocet++;

    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);

    bmeTeplota();
    bmeVlhkost();
    bmeTlak();
    oxid();
    
    pred=0;

    display.setTextSize(1);
    display.clearDisplay() ;
    display.setTextColor(SH110X_WHITE);

    if (pocet == 1) {
    display.clearDisplay() ;
    display.setCursor(28, 22);
    display.println("Time");
    } 

    if (pocet == 2) {
    display.clearDisplay() ;
    display.setCursor(11 , 8);
    display.print("Teplota: ");
    display.setCursor(66 , 8); 
    display.print(teplota);
    display.print(" ");
    display.print((char)247);                                     // symbol stupně
    display.println("C");

    display.setCursor(11 , 24);
    display.println("Tlak: ");
    display.setCursor(66 , 24);
    display.print(tlak);
    display.print(" hPa");

    display.setCursor(11 , 40);
    display.println("Vlhkost: ");
    display.setCursor(66 , 40);
    display.print(vlhkost);
    display.print(" %");
   
    display.setCursor(11 , 56);
    display.println("CO2: ");
    display.setCursor(66 , 56);
    if(CO2 >= 0)
    {
    display.print(CO2);
    display.print(F(" ppm"));
    }
    else
    {
    display.print(F("Preheating"));
    }
    }
    display.display();
  }

    else {
      pocet=0;
    }
  }
  
  else if (vibState == LOW) {
    pred=-1;
  }
}

void bmeTeplota() 
{
  teplota = bme.readTemperature();
}

void bmeVlhkost() 
{
  vlhkost = bme.readHumidity();
  if (vlhkost > 100)
    vlhkost = 100;
}

void bmeTlak() 
{
  tlak = bme.readPressure()/100;
}

void oxid ()
{
  int hodnota = analogRead(CO2Sensor);
  float napeti = hodnota*(3300/1023)+154;
  if (napeti < 400)
  {
    CO2 = -1;
  }
  else
  {
    CO2 = (napeti-400)*50/16;
  }
}

The point is that it changes, but the values don't update spontaneously

you have to specifiy this "it" what is "it"?

Which values don't change?

Best thing is to post what your display is showing
and
what you want the display to show

the values don't update spontaneously

Sorry, that phrase makes no sense, and is not helpful. Please describe what you expected to happen, and what happened instead.

[quote="syky16, post:8, topic:1224366"]

  int vibPin=0;

[/quote]

You might want to change this pin to other-than pin 0. Changed my mind. No Serial.prints.

Where did you get this? It is not even used.

#define SEALEVELPRESSURE_HPA (1013.25)

Switch the order of these two:

  display.display(); // THIS ONE
  delay(2000);
  display.clearDisplay() ;  // THIS ONE

The screen change from time to the second one, bit the values of BME (Temperatur, Humidity...) change ONLY when the screen change.

I want to change the screen and leave it, but for the BME values to update.

#define SEALEVELPRESSURE_HPA (1013.25)

from another attempt, I just didn't delete it. My bad

display.display(); // THIS ONE
  delay(2000);
  display.clearDisplay() ;  // THIS ONE

after switching, it still behaves the same

second picture is the time-screen

which screen? The time-screen?? or the sensor-screen??
what does leave it mean?
leave it as it is ?
or
changing to show something different?

"but for the BME-values to update?"
I don't understand at all !

You are talking in riddles

What is so hard about writing explicitly
I have a first screen that shows time and should show
......... explicitly writing what you want to see

I have a second screen that shows sensor-values and should show
......... explicitly writing what you want to see

example
If I shake the sensor while the sensor-screen is shown
then ..... explicitly writing what you want to see

writing in detail what you see instead

If I shake the sensor while the time-screen is shown
then ..... explicitly writing what you want to see

writing in detail what you see instead

You need this line after new information to make the screen update

Sorry, english is not my first language.

if the display shows the time, I want the time to be updated after one second (I don't have the time code yet)

when I shake the vibration sensor, I want the display to show the BME values, which will also update after maybe 5 seconds.

when I shake it again it changes back to time.

Now only the screen with the BME values that are changing is currently displayed
The screen will not change when shaken

I'm sorry for the bad wording

Your sketch has a "blank" screen as the first screen... is that where you will put the BME?
1

Then write the detailed description in your native language and use google-translate
The grammar will not be brilliant and a few words might be odd.
But the much more detailed description still give so much more information that it will be much better to understand. And nice side-effect if you read the english translation your english will improve

@xfpd

wow cool. What software did you use to create this animated gif?
please post the link to the WOKSim

I have trouble expressing my thoughts in a written sequence, so I use lists...

  1. Take anger management course.
  2. Open cooler door
  3. Enter
  4. Close cooler door
  5. Sit in cooler for a weekend
  6. Profit