Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

analogWrite by using proximity sensors

$
0
0

Good Evening,

 

I wrote a simple code which suppose to  take the input from proximity sensors and analogWrite it on microcontroller pins. I'm trying to take the value from right sensor and write that value on pin 12. Then take the value from left sensor and write it on pin 13. I'm using map() function to scale it down (10 bit to 8 bit).

 

But it take the value from one sensor and write this on both 12 and 13. So if I move my hand in front of right sensor (or left sensor) value on both 12 and 13 changes at the same time. Right sensor only suppose to adjust ouput waveform on pin 12. Left sensor suppose to only adjust the waveform on pin 13.

 

I tried with motors first. Then I tested the signals on oscilloscope. I got exact same result. 

 

I'm using Sharp IR proximity sensors (GP2Yxxxx). I tested the sensors using serial monitor and multimeter. They are working great.

 

 

Below is my code. Any help will be greatly appreciated.

 
 
int sensor_r = 0; //right IR proximity sensor
int sensor_l = 0; // left IR proximity sensor
int sensor_fr = 0; //front right IR proximity sensor
int sensor_fl = 0; //front left IR proximity sensor
 
 
void setup()
{  
  Serial.begin(9600); 
 
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  
}
void loop()
{
 
 int right_speed = analogRead(A7);
  int right = map(right_speed, 0, 1023, 0, 245);
  analogWrite(12, right);
  
   int left_speed = analogRead(A4);
  int left = map(left_speed, 0, 1023, 0, 235);
  analogWrite(13, left);
  
 
 
}
 
Thanks.
 
Regards,
Navi
 

Attached Files


Viewing all articles
Browse latest Browse all 2077

Trending Articles