Arduino Serial help

Hi friends.

First i apologize for my bad EN.

Now i have one question for Serial monitor in arduino.

I want to give any value from serial to arduino.

Example:

When i send "3" from serial monitor i want to give this value "3" on "x" ( x = 3) .

i try to make that whit this code:

int button = 9;
int led = 13;
int buttonState = 0;
int input = 0;
int counter = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(button);
  input = Serial.read();
  
  if (buttonState == HIGH)
  {
    counter++;
  }
  
  if (counter == input)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

i want to give value on "input" from serial monitor !

if (input >= '0' && input <='9')
{
  x = input - '0';
}

You can also just use Serial.parseInt() instead of Serial.read(). It's more straightforward.

AWOL:

if (input >= '0' && input <='9')

{
  x = input - '0';
}

ok i try with this but where put this ?

ilias_:
You can also just use Serial.parseInt() instead of Serial.read(). It's more straightforward.

yes this gve the value on "input" but the code with counter not working :slight_smile: when i give value on "input" i want the counter count to "input"

if (counter ==  input)
{
digitalWrite(led, HIGH);
}

We can't see your code, so it is difficult to help.

yes this gve the value on "input" but the code with counter not working smiley when i give value on "input" i want the counter count to "input"

well, not at all surprising, since that never happens in your code.

AWOL:
We can't see your code, so it is difficult to help.

That is my code :

int button = 9;
int led = 13;
int buttonState = 0;
int input = 0;
int counter = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(button);
  input = Serial.read();
  
  if (buttonState == HIGH)
  {
    counter++;
  }
  
  if (counter == input)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

i want to put value from serial monitor and when the counter count the value who i send from serial monitor torn on the led .
i show you:

if (counter == input)
{
digitalWrite(led, HIGH)
}

when i type 3 or other number the "input" get this number from the serial and when counter " ==" number from serial monitor turn on the led

your counter should increment every time you PRESS the button, that is when the button was LOW and is now HIGH.
Read this example: go to File>Examples>02.Digital>StateChangeDetection

ilias_:
your counter should increment every time you PRESS the button, that is when the button was LOW and is now HIGH.
Read this example: go to File>Examples>02.Digital>StateChangeDetection

Here is :

if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }

But i wnat to make this :

if (buttonPushCounter == x) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }

This "X" get value from the serial monitor and when i type and send 3 or other number this sended to "X" and example:
i send from serial monitor 3 and when "buttonPushCounter" count 3 times "digitalWrite(ledPin, HIGH);"

10x dudes :slight_smile: :%

I make that i want :smiley:

that is the code who i write and now is OK XD

const int  buttonPin = 9;    
const int ledPin = A0;       
int x = 0;
int input = 0;


int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;     

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


void loop() {

  buttonState = digitalRead(buttonPin);
  input = Serial.read();

  if (buttonState != lastButtonState) {
    
    if (buttonState == HIGH) {
      
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      
      Serial.println("off"); 
    }
  }
  
  lastButtonState = buttonState;
  if (input >= '0' && input <='9')
  {
    x = input - '0';
  }
  
  if (buttonPushCounter == x) {
    analogWrite(ledPin, 0);
  } else {
    analogWrite(ledPin, 255);
  }
  
}

now i want to know how to write 100 or higher number in the serial monitor ?

/*
 *  Serial input must end with a newline or a carriage return and a newline.
 *  So we know when input is ended.
 *  
 */

char inChar;

int inVal;

void setup()
{
  
  Serial.begin ( 9600 );
  Serial.println ( F ( __FILE__ ) );
  Serial.println ( F ( "Read ascii numeric input and convert to integer." ) );
  
} // setup


void loop ()
{
  
  while ( Serial.available () > 0 ) 
  {
    inChar = Serial.read ();  

    switch ( inChar )
    {
      
      case '\n': // end of data input, inVal is an integer, use as needed

        // print to serial out as demo
        Serial.println ( inVal );
        
        // reset inVal for next input
        inVal = 0;
      
      break; 
      
      case '0' ... '9': // capture the numeric ascii input and convert to integer
        
        inVal = ( inVal * 10 ) + ( inChar - '0' ); // ascii to integer
          
      break;

    }  // switch
      
  }  // while 
  
} // loop

tf68:

/*

*  Serial input must end with a newline or a carriage return and a newline.
*  So we know when input is ended.

*/

char inChar;

int inVal;

void setup()
{
 
  Serial.begin ( 9600 );
  Serial.println ( F ( FILE ) );
  Serial.println ( F ( "Read ascii numeric input and convert to integer." ) );
 
} // setup

void loop ()
{
 
  while ( Serial.available () > 0 )
  {
    inChar = Serial.read ();

switch ( inChar )
    {
     
      case '\n': // end of data input, inVal is an integer, use as needed

// print to serial out as demo
        Serial.println ( inVal );
       
        // reset inVal for next input
        inVal = 0;
     
      break;
     
      case '0' ... '9': // capture the numeric ascii input and convert to integer
       
        inVal = ( inVal * 10 ) + ( inChar - '0' ); // ascii to integer
         
      break;

}  // switch
     
  }  // while
 
} // loop

Ok now i maked this work with keypad :slight_smile: When i press 3 or other number in keypad the "X" get this value and when i push the button 3 times i the led turned off :slight_smile: But now i wont when i insert 100 or 200.... and hit the " * " or " # " to sed this value to this " X " :slight_smile: i try this with an example with keypad password, the example is when i type the password and hit the " * " or " # " password verify :slight_smile:

You can add other case statements.

case '*':
  do something with inVal
  reset inVal
break;

case '#':
  do some other thing with inVal
  reset inVal
break;

and so on...

tf68:
You can add other case statements.

case '*':

do something with inVal
  reset inVal
break;

case '#':
  do some other thing with inVal
  reset inVal
break;




and so on...

10x :slight_smile: it works.

OK All works but now i have one question. Now i want with keypad type only specifed numbers :slight_smile: I want to disable combination of this number:

11,12,13,14,15,16,17,18,19 end etc :slight_smile: and i want availbe only this number:

10,20,30,40,50,60,70,90,100 end etc. :slight_smile:

How to make it i have one idea but with this idea i will make 100 if statements :~

if ((x % 10) == 0)

AWOL:

if ((x % 10) == 0)

What is this ? please give me one example with this.

I just gave you an example.

Is it really too much effort for you to go look at a C reference, or even the Arduino reference page?