Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
If a2dconverter <threshhold(program in here)Else(Something else)
For my second problem/programHow do i start the program when it senses light under the throshold. At the moment i can get the sensor to detect light under the threshold but the servo is turning from the minute i turn on the power.And i am thinking i am also gonna need to stop the program when there is no ball in there and then start again when the ball is sensed.and my final question is, how do add a delay to my program as i need to add a delay when the platform has been lifted to its final height.Thanks for all your help.Joker94
Do some google searching for code examples, there are many available.
#include "SoR_Utils.h"#include "avr/io.h"#include "util/delay.h" int main(void) { int sensor_left=0; int threshold=100; configure_ports(); // configure which ports are analog, digital, etc. a2dInit(); // initialize analog to digital converter (ADC) a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage servo_left=0x00 while(1) { //store sensor data sensor_left=a2dConvert8bit(5); if (sensor_left< threshold) { servo_left( { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } return 1; } ) else if(sensor_left>threshold) { _delay_ms(100) { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } return 1; } return 0; }
while(1) { //store sensor data sensor_left=a2dConvert8bit(5); if (sensor_left< threshold) { servo_left( { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } return 1; } ) else if(sensor_left>threshold) { _delay_ms(100) { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } return 1; } return 0; }
while(1) { //store sensor data sensor_left=a2dConvert8bit(5); if (sensor_left < threshold) { servo_left() DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } } else if(sensor_left > threshold) { _delay_ms(100) DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } } }
#include "SoR_Utils.h"#include "avr/io.h"#include "util/delay.h" int main() { int sensor_left=0; int threshold=100; configure_ports(); a2dInit(); // initialize analog to digital converter (ADC) a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltagewhile(1) { //store sensor data sensor_left=a2dConvert8bit(5); if (sensor_left < threshold) { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } } else if(sensor_left > threshold) { _delay_ms(100); } { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } } } }
#include "SoR_Utils.h"#include "avr/io.h"#include "util/delay.h" int main() { int sensor_left=0; int threshold=50; configure_ports(); a2dInit(); // initialize analog to digital converter (ADC) a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltagewhile(1) { //store sensor data sensor_left=a2dConvert8bit(5); if (sensor_left < threshold) { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } } else if(sensor_left > threshold) { _delay_ms(100); } { DDRD = 0xff; PORTD = 0xff; for(int i=0; i < 100; i++) { PORTD = 0xff; _delay_ms(2); PORTD = 0x00; _delay_ms(20); } } } return 0;}
Comments would help us know what you are thinking the code should do. Other wise we must guess.
I guess you have the servo connected to one of the pins on PortD. Then you are sending a 2msec high pulse to the servo, outputting a low for 20msec and doing this 100 times. Is your servo un-modified and goes to a position proportional to the pulse width or is it a servo modified for continuous rotation?
Do you have any LEDs connected to an output port. These can be helpful in debugging. For example: ligth the LED if the threshold is lower than the light level and turn it off when the threshold is higher than the light level.
You do the exact same values for both above and below the threshold. So the servo does the same thing regardless of the light level. Is this what you want? Why all 8 pins of the port?