Code getting me crazy

Hi you guy some one can please give an light about this piece of code that are working erraticaly.

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int ps,pf,f,s,fan,ir,temp;
int tpma,tpmi,th,tm,da,luz,luzpwm,th1;
unsigned long T,p;
void setup() {
lcd.begin(16, 2); 
pinMode(12,INPUT_PULLUP);//flexa
  pinMode(8,INPUT_PULLUP);//setting
  pinMode(9,OUTPUT);//aquecedor
  pinMode(10,OUTPUT);//fan
  pinMode(11,OUTPUT);//luz da telinha
  pinMode(13,OUTPUT);//luz
}

void loop() {
 s=digitalRead(8);
digitalWrite(11,LOW);
lcd.clear();
if (s==LOW and s!=ps)
{
  ps=s;
  telae();

}
ps=s;
}
void telae()
 {
  
  int x=0;
  while (x==0)
  {
    p=millis();
    s=digitalRead(8);
    digitalWrite(11,HIGH);
  lcd.clear();
  //lcd.print(rtc.getTimeStr());
  lcd.setCursor(0,1);
  lcd.print("Fan");
  lcd.print(fan);
  lcd.print("%");
  lcd.print(" ");
  lcd.print(temp);
  lcd.print((char)223);
  lcd.print("C");
  f=digitalRead(12);
  delay(80);
  if (s!=ps)
  {
    if ( s==LOW)
    { 
      ps=s;
      telam();
      x=1;
    }
  }
  ps=s;
  
  }
 }

 void telam ()
{
  int x=0;
  T=p;
  int ss =0;
  while (x==0)
  {
    p=millis();
    digitalWrite(11,HIGH);
  delay(80);
  s=digitalRead(8);
  f=digitalRead(12);
  if (f!=pf)
  {
    if(f==LOW)
    {
      ss++;
      T=p;
    }
    if(ss>5)
    {
    ss=0;
    }
  }
  pf=f;

  // chamando as telas do menu ++++++++++++++++++++++
  if(ss==0)
  {
    tela1();
  }
  if (ss==1)
  {
    tela2();
  }
  if(ss==2)
  {
    tela3();
  }
  if(ss==3)
  {
    tela4();
  }
  if(ss==4)
  {
    tela5();
  }
  if(ss==5)
  {
    tela6();
  }
  if (s!=ps)
  {
  if (ss==0 and s==LOW)
  {
    ps=s;
    //fan_sp();
    x=1;
  }
  
 
  
    if(ss==1 and s==LOW)
    {ps=s;
      //IR();
      x=1;
    }
    if(ss==2 and s==LOW)
    {
      ps=s;
      tmp();
      x=1;
    }
    if(ss==3 and s==LOW)
    {ps=s;
      //timer();
      x=1;
    }
    if(ss==4 and s==LOW)
    {
      ps=s;
    
      //clock_seth();
      x=1;
    }
    if (ss==5 and s==LOW)
    {ps=s;
      telae();
      x=1;
    }
  }
 
ps=s;
  }
}

void tela1()
{
  lcd.clear();
  lcd.print("Fan speed");
  lcd.print("<");
  lcd.setCursor(0,1);
  lcd.print("LED");
}

void tela2()
{
  lcd.clear();
  lcd.print("Fan speed");
  lcd.setCursor(0,1);
  lcd.print("LED");
  lcd.print("<");
}

void tela3()
{
  lcd.clear();
  lcd.print("Temperature");
  lcd.print("<");
  lcd.setCursor(0,1);
  lcd.print("Timer");
  
}

void tela4()
{
 lcd.clear();
  lcd.print("Temperature");
  lcd.setCursor(0,1);
  lcd.print("Timer");
  lcd.print("<");

}

void tela5()
{
  lcd.clear();
  lcd.print("clock");
  lcd.print("<");
  lcd.setCursor(0,1);
  lcd.print("information");
}

void tela6()
{
   lcd.clear();
  lcd.print("clock");
  lcd.setCursor(0,1);
  lcd.print("information");
  lcd.print("<");
}

void tmp()
{ 
  int sss=0;
  int x=0;
  while(x==0)
{
  delay(80);
  s=digitalRead(8);
  f=digitalRead(12);
  if (s!=ps)
  {
    if (s==LOW);
    {
      sss++;
    }
  }
  ps=s;
  if(sss==2)
  {
   x=1;
  }
  if (sss==0)
  {
  tp_max();
 delay(1000);
  }
  if (sss==1)
  {
  tp_min();
  }
  if(f!=pf)
  {
    if (f==LOW and sss==0)
    {
    tpma++;
    if (tpma>35)
    {
      tpma=20;
    }
  }
  }
  if(sss==0)
  {
  pf=f;
  }
  if(f!=pf)
  {
    if(f==LOW and sss==1)
    {
    tpmi++;
    if(tpmi>24)
    {
      tpmi=15;
    }
  }
  if (sss==1)
  {
    pf=f;
  }
  if(sss==2)
  {
    x=1;
  }
 }
 
}
}

void tp_max()
{
  lcd.clear();
  lcd.print("temp, max");
  lcd.setCursor(0,1);
  lcd.print(tpma);
  lcd.print((char)223);
  lcd.print("C");
  
  
}
void tp_min()
{
  lcd.clear();
  lcd.print("temp,min");
  lcd.setCursor(0,1);
  lcd.print(tpmi);
  lcd.print((char)223);
  lcd.print("C");
  
  
}

please

Can you please give light on what that means?

Code getting me crazy

Me too.

Hello Danilo29

In general, you should not use magic numbers. The I/O pins love to have a functional name.
Use variable names with functional name too.

Have a nice day and enjoy coding in C++.

The code is not working erratically, it is working exactly as it should. That may not be how you wanted it to work. We do not know how you wanted it to work. What we do know is that the less experience and knowledge the coder has, the more the chance that the code will not do what the coder wanted. We can also tell that you do not have a high level of experience and knowledge. Poor indentation, poor naming of variables and functions, lack of comments and repetitive code are some of the signs of that.

Please explain what you wanted this code to do, and also what it is doing.

Also please fix the indentation with Tools->Auto Format, add more comments (in English please, if you would prefer to use your own language, we can move your topic to a section of the forum where others will understand it better).

1 Like

Or, that someone else, or chatGPT, doesn't have it. No author was mentioned.

Come to think of it, that does look like AI code. "we have 32 gigs of memory, why not waste 5 or 6 to make it easier and less bothersome" :roll_eyes:

Wouldn't an AI know that using single letter variable names doesn't save any of that memory? :wink:

If the OP makes the changes suggested in reply #3, there might be some chance of help.

It's not a bug it's a wad.. (working as designed)..
um, your variable names leave much to be desired..
joined 5 years ago, no posts until now..
forget about ChatGPT, I think we got a new type of bot..
sorry, have fun.. ~q

Agreed, sorry for the sarcasm. :zipper_mouth_face:

Not according to the person who is asking...

i'm not convinced it is a person..

ok lol im need to know way this code is not working.

In that case, please read the forum instructions on what to post, to increase your chances of getting an answer:

The answer in reply #3 was spot on, 99% of programmers won't even look at code like this, without those improvements. So please go and make them now.

Ok i see sorry english is not my first language, i dont put name in my pins because i cant write fast enough, so to not take to much time i just put numbers sory if is get difficult

Hi @Danilo29

welcome to the arduino-forum.

Well done to post the code as a code-section in your very first post.

You seem to be somehow frustrated that the code does not work as you wanted.

The other users have mentioned what you should do.

  • using self-explaining names for the variables
  • adding comments
  • describing what project this is
  • describing the behaviour of the code what you observe and how this deviates from what you want.

I have no idea in what field you yourself are an expert in.
Imagine somebody comes to you saying

my "X" behaves erratically what should I do?
And the only information this person is giving to you for analysing is

"my hair is green and I live in XY"

You have to ask back for all the details that are nescessary for an analysis

best regards Stefan

2 Likes

Your haste should not become our problem. Please fix it.

1 Like

Is the code not working or working erratically?

What does not working mean?

What does working erratically mean?

I tell you what this code is working erratically:

#include <stdint.h>
#include <pru_cfg.h>
#include "resource_table_empty.h"
#include "prugpio.h"

volatile register unsigned int __R30;
volatile register unsigned int __R31;

void main(void) {
	int i;
	
	uint32_t *gpio1 = (uint32_t *)GPIO1;
	CT_CFG.SYSCFG_bit.STANDBY_INIT = 0;
	for(i=0; i<100; i++) {
		gpio1[GPIO_SETDATAOUT]   = USR1;	
		gpio1[GPIO_CLEARDATAOUT] = USR2;	
		__delay_cycles(500000000/5);	 
		gpio1[GPIO_CLEARDATAOUT] = USR1;	
		gpio1[GPIO_SETDATAOUT]   = USR2;	
		__delay_cycles(500000000/5); 
	}
	__halt();
}

#pragma DATA_SECTION(init_pins, ".init_pins")
#pragma RETAIN(init_pins)
const char init_pins[] =  
	"/sys/class/leds/beaglebone:green:usr1/trigger\0none\0" \
	"/sys/class/leds/beaglebone:green:usr2/trigger\0none\0" \
	"\0\0";

Can you determine what, by looking at the code, does working erratically mean?

In which case @Danilo29 doesn't have l have it either, otherwise they would have fixed it! :slight_smile:

thanks for this explanation, i tought i have experience lol.

Okay so let me explain what is goin on.

this code is suposet to be an menu to control temperature clock light etc...

but when i upload to the arduino, the screens can be selected for example

temperature <
Light

when i push the botton

it changes
temperature
light<

when i press the button setting

it enter on the setting menu
but the problem start at here

 if (sss==0)
  {
  tp_max();
 delay(1000);
  }

it jump to this part of the code

if (sss==1)
  {
  tp_min();
  }

even whitout i press arrow button

and dont even respect the delay of 1000 ms

some time respect, some time dont.

after the value of minimum temperature are settled, the code act totally errant,

the screen of the information start to blink whenever i press the button set but it not enter in the iddle screen.

SO that is the problem i could find anything wrong in the code to that bug happens

OK so we knew some more about what your code does

I used google translate to find out what language this is
pinMode(9,OUTPUT);//aquecedor
pinMode(10,OUTPUT);//fan
pinMode(11,OUTPUT);//luz da telinha

google says portuguese

There is a spanish speaking sub-forum.
It might be easier for you to let a moderator move your thread to the spanish sub-forum.

You are talking about

So which IO-pin is your "arrow"-button connected to???

This is the reason why you should use meaningful and self-explaining names for everything.

If you post - in a new posting - a revised code that
uses meaningful and self-explaining names for everything I will take a look into your code
otherwise not

best regards Stefan

1 Like