Can you help me with phototransistor led motion detector

Hello everyone, I have to do a motion detector and count motion using phototransistor and led using C (not Arduino) language, but using arduino bradboard.
I implemented this circuit, but on the code part I have some troubles.

I will be so happy if anyone could help me.
Thanks

Your post was MOVED to its current location as it is more suitable because it is not an introductory tutorial

ok, thanks

There is no such thing as the Arduino language. Arduino boards are programmed in C, C++ and in the case of some (not really) Arduino boards such as the ESP32 in Python

Please post the exact details of what sounds like an assignment

What coding have you tried so far and where are you stuck ?

EDIT : corrected spelling

void initializare_COMP(){
ACSR= 0b00001000; // AIN0 = (Vin+); AIN1 = (Vin-);
ISR(ANALOG_COMP_vect) 
{
unsigned char data;
//_delay_ms(10); 
data = ACSR & 0x20; 
if (data != 0)
{
ON_LED_3;//LED ON
N=1; 
data = TCCR2 & 0xF8;
TCCR2 = data | 0x05; //start clk Timer PWM
}
else
{
OFF_LED_3;//LED OFF
N=0; 
data = TCCR2 & 0xF8;
TCCR2 = data | 0x00; //stop clk PWM
PORTB &=~(1<<7); 
}
}

i have this example but i don know how to implement in my case

Thanks for using code tags when you posted

What do you expect that function to do ?

ON_LED_3;//LED ON

What do you expect the lines like this to do ?
Which Arduino board do you have ?

when data is not 0 ( is 1) i understand that led is on but i am not sure.

its an "if" statement, and I am actually ready to change the code but I dont know how to start and I am not so good at coding unfortunately :frowning:

Arduino uno

that code which i sent is for this circuit

How is ON_LED_3; an if statement ?

Hi, @psionyx
Welcome to the forum.

Can you explain?
How do you load it onto the UNO?
Why not use the Arduino IDE to do your code?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

oh, sorry my bad, this is the code

/*
 *	Controller de lumina cu fotodioda 
	fotodioda BPW34
	Comparator si generator PWM.
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
#include <util/delay.h>

#define ON_LED_L PORTB |= 0x20
#define OFF_LED_L PORTB &= 0xDF


const unsigned char Sir1[] = {"\r\nFotodioda, Detector de lumina\r\n"};
const unsigned char SirON[] = {"\r\nLumina\r\n"};	
const unsigned char SirOFF[] = {"\r\nIntuneric\r\n"};
	
	
const unsigned char SirP[] = {"="};



//#define ADC_bipolar //ADC lucreaza in modul bipolar

unsigned int N,INEL_DATE[80];  //memoria inel
unsigned char display_Chars_10[10]={'=','2', '5','6' ,'4',',', '9','9', 0x0D, 0x0A};

unsigned long int V ;
//programarea porturilor
 void  Initializare_porturi()
 {
	 DDRC = 0x00;  //bit 0 high Z
	 PORTC = 0x00;

	 DDRB = 0x28; //LED si 0C2
	 PORTB = 0x11;

	 DDRD &= 0x90; //AIN0, AIN1
	 PORTD &= 0x90;//AIN0, AIN1
 }
 void RESET_Var()
 {
	 N = 0;
 }
void Trimite_Mesaj(const unsigned char *pointer)
{
	const unsigned char *String;
	String = pointer;
	while (*String != 0)
	{
		while ((UCSR0A & (1 << UDRE0)) == 0) {};
		UDR0 = *String++;
	}
}
void Trimite_Valoare (unsigned char cmd, unsigned long int valoare)
{ 
 unsigned char Local_vr = 0;
 unsigned long int data_int=0, Diplay_Val;
 Local_vr = cmd;
 Diplay_Val  = valoare;
 if (Local_vr !='m')
 {
	 for (Local_vr=0;Local_vr<10;Local_vr++)
	 {
		 display_Chars_10[Local_vr] = 48;  //0 0 0 0 0 ..
	 }
	 
	 data_int = Diplay_Val;
	 display_Chars_10[0] ='=';
	 display_Chars_10[9] =0x0A;
	 display_Chars_10[8] =0x0D;
	 display_Chars_10[5] =0x2E;
	 
	 for (Local_vr=7;Local_vr>=0;Local_vr--)
	 {
		 if (data_int !=0)
		 {
			 if (Local_vr!=5)
			 {
				 display_Chars_10[Local_vr] = (data_int%10)+48;
				 data_int = data_int/10;
			 }
		 }
		 else break;
		 
	 }

	 UCSR0A |=0x80;
	 UDR0 = cmd;
	 
	 for (data_int=0;data_int <10; data_int++)
	 {
		 
		 Local_vr = UCSR0A &  0x20;
		 while(Local_vr ==0) Local_vr = UCSR0A & 0x20;
		 UCSR0A |= 0x20;
		 UDR0 = display_Chars_10[data_int];
	 }
 }
 else  // afisez datele din memorie
 {
	 for (data_int=0;data_int<80;data_int++)
	 {
		 Local_vr = UCSR0A &  0x20;
		 while(Local_vr ==0) Local_vr = UCSR0A & 0x20;
		 UCSR0A |= 0x20;
		 UDR0 = (INEL_DATE[data_int])>>8;
		 
		 Local_vr = UCSR0A &  0x20;
		 while(Local_vr ==0) Local_vr = UCSR0A & 0x20;
		 UCSR0A |= 0x20;
		 UDR0 = INEL_DATE[data_int];
	 }
 }
 }
 void initializare_COMP()
 {
	
		ACSR= 0b00001000; // ACIE = 1, AIN0 = (Vin+); AIN1 = (Vin-);		
		
 }
   
  void  Initializare_timer2_PWM()
  {
	  TCCR2A = 0b11000001;				//PWM_OFF (| 0xC0 start PWM)Set OC2A on compare match
	  TCCR2B  =0x00;
	  TCCR2B  |=0x00;						//clkI/O/128			
	  OCR2A = 128;						//pwm 50%
  }

  void Initializare_SERIAL()
  {
	  UCSR0B  = 0b10011000;
	  UBRR0H = 0;    //
	  UBRR0L = 103;  //9600biti/sec
  }
void Semnalizare_Start()
{
	unsigned char data;
	for(data=0;data<4;data++)
	{
		ON_LED_L;
		_delay_ms(200);
		OFF_LED_L;
		_delay_ms(200);
	}
	_delay_ms(200);
}

int main(void)
{
	Initializare_porturi();
	Initializare_timer2_PWM();
	Initializare_SERIAL();	
	initializare_COMP();
	
	Semnalizare_Start ();  //semnalizez luminos start program
	Trimite_Mesaj(Sir1);
	sei();
	
    while (1)
    {
		//bucla principala, astept evenimente pe intrerupere (tasks)
    }
}
// ON LED3 si PWM la riding edge- off LED3 si PWM  la failing edge
ISR(ANALOG_COMP_vect) //intreruperea comparator 
{
	unsigned char data;
	//static unsigned int Index_1ms;
	//Index_1ms++;
	//if (Index_1ms <= 500)
	//{
		
		//ON_LED_L;
	//} 
	//else
	//{
	//}
	
	
	//_delay_ms(10);  //debounsing, echivalent histerezei// 
	data = (ACSR & (1<< ACO));//0x20;  //citesc iesirea comparatorului
	if (data != 0)
	{
		ON_LED_L;//pwm ON
		N=1;
		TCCR2B |= 0x05; //start clk Timer PWM 
	} 
	else
	{
		OFF_LED_L;//pwm OFF
		N=0;
		data = TCCR2B & 0xF8;
		TCCR2B = data | 0x00; //stop clk PWM
		PORTB &=~(1<<7);  // eroare aici OC2 ramine in 1 nu poate fi controlet daca PWM activ//  
	}					  //eventual disable PWM
}
 
 ISR(USART_RX_vect) {
	 
	 //afisez 99999 = 999.99*100;  folosesc 2 zecimale
	 //intreruperea de receptie seiriala
	 unsigned char data=0;
	 ON_LED_L;// testez rutina rx
	 
	 data = UDR0; //citesc comanda receptionata
	 
	 switch(data)
	 {
		 case 'n':
		 Trimite_Valoare('n', N);
			if (N != 0)  Trimite_Mesaj(SirON);
			else  Trimite_Mesaj(SirOFF);

		 break;
		 case '0':
		 Trimite_Valoare('0',N );
		 RESET_Var();
		 break;
		 default:
		 OCR2A = data;
	 }
	 
	 OFF_LED_L;
 }


Hello Tom,
I posted the right code in the message before
(if need i can post one more time, but dont wanna make chat very loaded)

It does not seem to have ON_LED_3 in it

that one was wrong code, sorry

I inserted the good code higher

Then I suggest that you delete the post with the wrong code in it or update the post with the correct code and add a note to explain what you have done

Hi,

Why?
Is this a school/educational establishment project/assignment?

Tom... :grinning: :+1: :australia:

And that circuit will behave differently to what you have.
Can you please tell us your electronics, programming, arduino, hardware experience?
Do you have a DMM?
If so have you measured the voltage on A0 to make sure your hardware is working?
Verify that the voltage changes as to cover and uncover the sensor?

Can you please tell us your electronics, programming, arduino, hardware experience.

Tom... :grinning: :+1: :coffee: :australia:

This Topic has been locked as it is the identical or too similar your other post.
Please use the original post and do not duplicate.