system
February 15, 2014, 9:00am
1
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 !
system
February 15, 2014, 9:29am
2
if (input >= '0' && input <='9')
{
x = input - '0';
}
system
February 15, 2014, 9:32am
3
You can also just use Serial.parseInt() instead of Serial.read(). It's more straightforward.
system
February 15, 2014, 9:47am
4
AWOL:
if (input >= '0' && input <='9')
{
x = input - '0';
}
ok i try with this but where put this ?
system
February 15, 2014, 10:30am
5
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 when i give value on "input" i want the counter count to "input"
if (counter == input)
{
digitalWrite(led, HIGH);
}
system
February 15, 2014, 11:35am
6
We can't see your code, so it is difficult to help.
system
February 15, 2014, 11:54am
7
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.
system
February 15, 2014, 12:37pm
8
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
system
February 15, 2014, 1:08pm
9
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
system
February 15, 2014, 2:51pm
10
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);"
system
February 15, 2014, 3:14pm
11
10x dudes :%
I make that i want
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 ?
tf68
February 15, 2014, 9:44pm
12
/*
* 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
system
February 19, 2014, 5:31pm
13
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 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 But now i wont when i insert 100 or 200.... and hit the " * " or " # " to sed this value to this " X " i try this with an example with keypad password, the example is when i type the password and hit the " * " or " # " password verify
tf68
February 20, 2014, 12:47am
14
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...
system
February 20, 2014, 2:59pm
15
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 it works.
system
February 22, 2014, 10:09am
16
OK All works but now i have one question. Now i want with keypad type only specifed numbers I want to disable combination of this number:
11,12,13,14,15,16,17,18,19 end etc and i want availbe only this number:
10,20,30,40,50,60,70,90,100 end etc.
How to make it i have one idea but with this idea i will make 100 if statements :~
system
February 22, 2014, 11:50am
18
AWOL:
if ((x % 10) == 0)
What is this ? please give me one example with this.
system
February 22, 2014, 12:03pm
19
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?