Go Down

Topic: Continuous serial read (Read 816 times) previous topic - next topic

acrossell

I'm currently combining tutorials on motors with serial comms. Im trying to read the the value of the motorPin and println to serial. This seems to be working except for the fact that the value is only read at the end of the loop cycle. How can my code read the value and print to serial  continuously as it changes rather than just at the end of the loop?

Thanks

Code: [Select]
//Define Pins
int motorPin = 9;
void setup()
{
  //Set the PWM Motor pin as an output
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);

}

void loop()
{

   //Increase Motor Speed from 0 -> 255
  for (int i=0; i<=255; i++)
  {
    analogWrite(motorPin, i);
    delay(10);
  }
 
  delay(0); //Hold it!
 
  //Decrease Motor Speed from 255 -> 0
  for(int i=255; i>=0; i--)
  {
    analogWrite(motorPin, i);
    delay(10);
  }
 
  delay(0); //Hold it! ;
 
  //print value of motor to serial
 
  int val = analogRead(motorPin) ;
 
  while (Serial.available() ==1) ;
  Serial.println(val) ;
  delay(50) ;
 
Serial.flush();
 
}



AWOL

#1
Mar 29, 2011, 06:00 pm Last Edit: Mar 29, 2011, 06:13 pm by AWOL Reason: 1
Code: [Select]
for(int i=255; i>=0; i--)
 {
   analogWrite(motorPin, i);
   Serial.println (analogRead (motorPin));
   delay(10);
 }

Or, have a look at the Blink Without Delay example to see how to do it without delay.

(Should that be "Or, have a look at the Blink Without Delay example to see how to do it, without delay." ?
Must ask Lynne Truss)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

johnwasser

Look at the "Blink Without Delay" for an example of how to do scheduled things (like increasing and decreasing motor speed every 10 miliseconds) without putting in delays.  Then you can do serial i/o at whatever interval you like while still making steady changes to the PWM output.

Note:  analogRead() only works on the Analog Input pins, not the PWM Output pins.  If you want to send the value you just sent to the PWM pin you should save that value:

Code: [Select]

if ('time to increase the motor speed)
   {
   speed++;
   analogWrite(motorPin, speed);
  }

if (time to send the speed value)
   Serial.println(speed);
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

AWOL

Quote
analogRead() only works on the Analog Input pins, not the PWM Output pins.

oops - missed that biggy!
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

retrolefty

Lots of logical errors and unneeded statements. However the biggest problem is that you are trying to do a analogRead() on pin 9 ? There are only 6 analog input pins (0-5) and it's not clear what you might have wired to a analog input pin, if anything? Your pin 9 is being used as a PWM output pin to power a motor at variable speed. Reading back that value makes little sense as it's only a digital on/off signal of variable duty cycle that wouldn't signify anything even if you did a digitalRead of that pin.

So before even trying to clean up your sketch, what are you trying to read back in your analogRead() statement? Do you really have an external analog voltage avalible to read into your sketch?

Lefty



acrossell

#6
Mar 29, 2011, 07:39 pm Last Edit: Mar 29, 2011, 07:42 pm by acrossell Reason: 1
managed to get what I was looking and clean it up a little, gonna try the suggestions regarding avoiding the use of delays. Cheers all, really helpful!

Code: [Select]
//Define Pins
int motorPin = 9 ;
int i ;

void setup()

{
 //Set the PWM Motor pin as an output
 pinMode(motorPin, OUTPUT);
 Serial.begin(9600);
}

void loop()
{

  //Increase Motor Speed from 0 -> 255
 for (int i=0; i<=255; i++)
 {
   analogWrite(motorPin, i);
   Serial.println(i) ;
   delay(10) ;
  }
 
 delay(1000); //Hold it!
 
 //Decrease Motor Speed from 255 -> 0
 for(int i=255; i>=0; i--)
 {
   analogWrite(motorPin, i) ;
   Serial.println(i) ;
   delay(10) ;
 }
 
 delay(1000); //Hold it! ;

AWOL

Code: [Select]
analogRead(i) ;

You don't have 256 analogue inputs.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

acrossell

#8
Mar 29, 2011, 07:43 pm Last Edit: Mar 29, 2011, 07:45 pm by acrossell Reason: 1
haha literately just took that line out too, I'm starting to get it a little bit, sure you have realised this is all pretty new to me. I cant code at all so i'm just ripping tutorials apart and trying to see how it all ticks.

Go Up