Go Down

Topic: how to make counter in this phase ? (Read 290 times) previous topic - next topic

heshammo

May 18, 2019, 01:40 pm Last Edit: May 18, 2019, 02:41 pm by heshammo
i have 3 button  i made 2 switch up and down to control on motor  i want to control them by last button to turn off  their function  when i pushed it high , make them turn actuator up and down and all this showed on LCD ?

Code: (c++) [Select]


#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
int a  = 11;
int b  = 12;
int c = 13;
int d = A4;
int val = 0;
int counter = 0;
int currentState = 0;
int previousState = 0;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
#define Bck       10
int read_LCD_buttons(){               // read the buttons
   adc_key_in = analogRead(0);       // read the value from the sensor

   // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
   // we add approx 50 to those values and check to see if we are close
   // We make this the 1st option for speed reasons since it will be the most likely result

   if (adc_key_in > 1000) return btnNONE;
  
    if (adc_key_in < 50)   return btnRIGHT;  
    if (adc_key_in < 195)  return btnUP;
    if (adc_key_in < 380)  return btnDOWN;
    if (adc_key_in < 555)  return btnLEFT;
    if (adc_key_in < 790)  return btnSELECT;  
  
   return btnNONE;                // when all others fail, return this.
}
int voltPin=A5;
//////////////////////////////////////////////////////////////////
// it's a 16x2 LCD so...
int screenWidth = 16;
int screenHeight = 2;

// the two lines
// line1 = scrolling
String line1 = "Easy Life Company - 01001004863";
String line2 = " Whizzzkid Inc. ";


// just some reference flags
int stringStart, stringStop = 0;
int scrollCursor = screenWidth;




//////////////////////////////////////////////////////////////////







void setup()
{
 Serial.begin(9600);
 pinMode(Bck,OUTPUT);
 pinMode(a,OUTPUT); //13
 pinMode(b,OUTPUT);//12
 pinMode(c,OUTPUT);
 pinMode(d,OUTPUT);
 pinMode(btnRIGHT,INPUT);
 lcd.begin(16, 2);               // start the library
 lcd.setCursor(0,0);             // set the LCD cursor   position
 analogWrite(Bck,255);
}






void loop()
{
            
  lcd_key = read_LCD_buttons();   // read th
  if (lcd_key == btnDOWN && lcd_key == btnUP&& lcd_key == btnRIGHT  )
            { lcd.setCursor(0,1);
              lcd.print("Eror                ");  //  push button "DOWN" and show the word on the screen
              digitalWrite(a,LOW);
              digitalWrite(b,LOW);
              digitalWrite(c,LOW);
              digitalWrite(d,LOW);
     }
    
  else if (lcd_key == btnUP)
             {
            lcd.setCursor(0,1);              
              lcd.print("Up Stairs                ");  //  push button "UP" and show the word on the screen
              digitalWrite(a,HIGH);                
              digitalWrite(b,LOW);

                                  }

  
  else if (lcd_key == btnDOWN)
            {
             lcd.setCursor(0,1);
            lcd.print("Down Stairs                ");  //  push button "DOWN" and show the word on the screen
            digitalWrite(a,LOW);
            digitalWrite(b,HIGH);

                             }
                            
             else if (lcd_key == btnRIGHT){
              lcd.setCursor(0,1);              
             lcd.print("actuator mode                ");  //  push button "UP" and show the word on the screen
             currentState = 1;

            
               }
              
              else  if (lcd_key == btnUP)
            {
            
             lcd.setCursor(0,1);
             lcd.print("actuator up                ");  //  push button "DOWN" and show the word on the screen
             digitalWrite(c,LOW);
             digitalWrite(d,HIGH);

            }
            
            
            
          else  if (lcd_key == btnDOWN)
            {
             lcd.setCursor(0,1);
             lcd.print("actuator down                ");  //  push button "DOWN" and show the word on the screen
             digitalWrite(c,LOW);
             digitalWrite(d,HIGH);

            }
                                  
                                  
                    
else if (lcd_key ==btnNONE )
            
            {
            digitalWrite(a,LOW);
            digitalWrite(b,LOW);
           digitalWrite(c,LOW);
            digitalWrite(d,LOW);




  
         /////////////////////////////////////////////////////////////////
              lcd.setCursor(scrollCursor, 1);
 lcd.print(line1.substring(stringStart,stringStop));
 delay(300);
 lcd.clear();
 if(stringStart == 0 && scrollCursor > 0){
   scrollCursor--;
   stringStop++;
 } else if (stringStart == stringStop){
   stringStart = stringStop = 0;
   scrollCursor = screenWidth;
 } else if (stringStop == line1.length() && scrollCursor == 0) {
   stringStart++;
 } else {
   stringStart++;
   stringStop++;
 }


            /////////////////////////////////////////////////////////
            
            
           // lcd.setCursor(0,1);
            lcd.print("Break!                        ");
            //lcd.print();
            /////////////////
            
float val = analogRead(voltPin);
float VoltRead= map(val, 346, 679, 18.4, 37);
//lcd.print(VoltRead);
//lcd.print("  ");
//lcd.print(val);
  




///////////////////////////////////                   //TESTING PATTERNS





  //////////////////////////////////////////////////////////////////////////////
  
  if(VoltRead<23)               /////Less than 23
  {
   lcd.setCursor(0,0);
  lcd.print("Battery:");
   lcd.setCursor(9,0);
   lcd.print("20%");}

   }

AWOL

Please don't cross post.

Duplicate deleted.

Please remember to use code tags when posting code

heshammo


AWOL

#3
May 18, 2019, 02:48 pm Last Edit: May 18, 2019, 02:48 pm by AWOL
please help me   :)
Can I introduce you to the IDE's auto-format tool?

It makes visualisation of the structure of your code much easier.

heshammo

#4
May 18, 2019, 03:04 pm Last Edit: May 18, 2019, 03:15 pm by heshammo
i used it but i miss understand what do you mean  :(

Code: (c++) [Select]


#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
int Pulse  = 11;
int Dir  = 12;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
#define Bck       10
long delay_Micros = 1800; // Set value
long currentMicros = 0; long previousMicros = 0;
int read_LCD_buttons() {              // read the buttons
  adc_key_in = analogRead(0);       // read the value from the sensor

  // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  // We make this the 1st option for speed reasons since it will be the most likely result

  if (adc_key_in > 1000) return btnNONE;

  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;

  return btnNONE;                // when all others fail, return this.
}
int voltPin = A5;
//////////////////////////////////////////////////////////////////
// it's a 16x2 LCD so...
int screenWidth = 16;
int screenHeight = 2;

// the two lines
// line1 = scrolling
String line1 = "Easy Life Company - 01001004863";
String line2 = " Whizzzkid Inc. ";


// just some reference flags
int stringStart, stringStop = 0;
int scrollCursor = screenWidth;




//////////////////////////////////////////////////////////////////







void setup()
{
  Serial.begin(9600);
  pinMode(Bck, OUTPUT);
  pinMode(Pulse, OUTPUT); //13
  pinMode(Dir, OUTPUT); //12
  lcd.begin(16, 2);               // start the library
  lcd.setCursor(0, 0);            // set the LCD cursor   position
  analogWrite(Bck, 255);
  digitalWrite(Dir, LOW);

}






void loop()
{
  currentMicros = micros();

  lcd_key = read_LCD_buttons();   // read the buttons

  if (lcd_key == btnDOWN && lcd_key == btnUP)
  { lcd.setCursor(0, 1);
    lcd.print("Eror                ");//  push button "DOWN" and show the word on the screen
    digitalWrite(Pulse, LOW);
    digitalWrite(Dir, LOW);
  }

  else if (lcd_key == btnUP )
  { lcd.setCursor(0, 1);
    lcd.print("Up Stairs                ");  //  push button "UP" and show the word on the screen
    currentMicros - previousMicros >= delay_Micros;
    previousMicros = currentMicros;
    digitalWrite(Dir, HIGH);
    digitalWrite(Pulse, HIGH);
    delayMicroseconds(50);
    digitalWrite(Pulse, LOW);
  }

  else if (lcd_key == btnDOWN)
  {
    lcd.setCursor(0, 1);
    lcd.print("Down Stairs                ");  //  push button "DOWN" and show the word on the screen
    currentMicros - previousMicros >= delay_Micros;
    previousMicros =  currentMicros;
    digitalWrite(Dir, HIGH);

    digitalWrite(Pulse, HIGH);

    delayMicroseconds(50); //Set Value

    digitalWrite(Pulse, LOW);
  }
  else if (lcd_key == btnNONE )

  {
    digitalWrite(Pulse, LOW);
    digitalWrite(Dir, LOW);


    /////////////////////////////////////////////////////////////////
    lcd.setCursor(scrollCursor, 1);
    lcd.print(line1.substring(stringStart, stringStop));
    delay(300);
    lcd.clear();
    if (stringStart == 0 && scrollCursor > 0) {
      scrollCursor--;
      stringStop++;
    } else if (stringStart == stringStop) {
      stringStart = stringStop = 0;
      scrollCursor = screenWidth;
    } else if (stringStop == line1.length() && scrollCursor == 0) {
      stringStart++;
    } else {
      stringStart++;
      stringStop++;
    }


    /////////////////////////////////////////////////////////


    // lcd.setCursor(0,1);
    lcd.print("Break!                        ");
    //lcd.print();
    /////////////////

    float val = analogRead(voltPin);
    float VoltRead = map(val, 346, 679, 18.4, 37);
    //lcd.print(VoltRead);
    //lcd.print("  ");
    //lcd.print(val);

}


/////////////////}


//delay(100);}

UKHeliBob

Code: [Select]
  if (lcd_key == btnDOWN && lcd_key == btnUP)
How likely is it that lcd_key will equal two different values at the same time ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up