Go Down

Topic: can arduino serial.read() values?? (Read 965 times) previous topic - next topic

Tom@Jerry

i have a project whereby need to input value of rpm into a motor via arduiono..is there any sample coding for me as reference.?? i need to input value of rpm ex : when i input 255rpm = motor runs at 255pwm..

thankx in advance.... :smiley-roll:

bubulindo

Depends on how you send it.

Imagine you send it in ASCII, meaning you'll receive a string.

Code: [Select]


#define DESCRIPTOR    'P'
char temp[4];


temp[3] = '\0';


if (Serial.available() > 5) { //<descriptor><value><value><value><end character>
   if (Serial.read() == DESCRIPTOR) { //first is correct.
      for(int i = 0; i<3; i++) {
         temp[i] = Serial.read();
      }
   if (Serial.read() == '\n') { // the string is correct... sort of ...
      pwm = atoi(temp);
   }

analogWrite(pwm); //there you go...


However, you can avoid that if you control the software that sends the command. What are you using to send the command?
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Tom@Jerry

const int ledPin = 6;      // the pin that the LED is attached to

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
 
}

void loop() {

  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
   
   
  }
  Serial.println(brightness);
  delay(500);
}
----------------------------------------------------------------------------------------------------------------------------------------------------
i want to do like this, which mean during the serial monitor, i would like to insert "255" means max pwm... then the num "255: will eventually insert into one of the coding to initalize motor at pwm "255" pwm..huuuuu

thanx in advance...8)... i try yours bubulindo..but smehow i dont get it....

PaulS

Quote
i want to do like this, which mean during the serial monitor, i would like to insert "255" means max pwm... then the num "255: will eventually insert into one of the coding to initalize motor at pwm "255" pwm..huuuuu

You'll need to rewrite the Serial Monitor application, then.

The Serial Monitor takes whatever text you have keyed in, and sends it to the serial port.

Unless you intend to rewrite the Serial Monitor application, then you must rewrite the Arduino code to collect the string that the Serial Monitor application sends, and convert the string back to an int.


Nick Gammon

@fendywoo

Read this before posting a programming question

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Go Up