Go Down

Topic: how to input values through serial monitor.please help (Read 42 times) previous topic - next topic

christinawilson205

i have to put  values for desired value through a serial monitor .i'm using a dc motor which has a potentiometer feedback and a H-Bridge. The current value reads the values from the pot .
Code: [Select]

int enB = 5;
int pin1 = 7;
int pin2 = 6;
int pot=A0;
int CurrentValue;
int DesiredValue;
void setup()
{
  pinMode(enB, OUTPUT);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pot,INPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Enter values between 60 - 960");
}
void loop()
{
  if (Serial.available()){
    int DesiredValue = Serial.read();     
 CurrentValue = analogRead(A0);

 if (CurrentValue>DesiredValue)
  {
  //right 
  digitalWrite(pin1,HIGH);
  digitalWrite(pin2, LOW);
  analogWrite(enB, 100);
   }
if(CurrentValue<DesiredValue)
{
  //left
digitalWrite(pin1, LOW);
  digitalWrite(pin2, HIGH);
  analogWrite(enB,100); 
}


  if (DesiredValue < 10 && DesiredValue > -10)
  {
  analogWrite(enB,0);
 }
  } }

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.


There is a simple user-input example in Planning and Implementing a Program


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up