Operácie

Ovládanie RGB LED pásika: Rozdiel medzi revíziami

Zo stránky SensorWiki

StudentMIPS (diskusia | príspevky)
Bez shrnutí editace
StudentMIPS (diskusia | príspevky)
Bez shrnutí editace
Riadok 17: Riadok 17:
== Analýza  a opis riešenia ==
== Analýza  a opis riešenia ==


Zapojenie LED pasíka je jednoduché len tri vodiče a v data vstupom som dal resistor, aby náhodne veľké prúdy nepoškodili pása. Najťažšia časť bola nastavid pás aby poslané farby vykresloval, na to som použil asembler kód čo som našiel na internete. Na ovládanie som napísal malú menu s tromi zvláštnymi módmi.
Zapojenie LED pasíka je jednoduché len tri vodiče a v data vstupom som dal resistor, aby náhodne veľké prúdy nepoškodili pása. Najťažšia časť bola nastavid pás aby poslané farby vykresloval, na to som použil asembler kód čo som našiel na internete. Na ovládanie som napísal malú menu s tromi zvláštnymi módmi, používaním numpadu môžme navigovať cez módy prednastavené, manuálna nastavenia celého pása a manuálna nastavenia hocijakeho LEDa.


[[Súbor:ledschema.jpg|400px|thumb|center|Schéma zapojenia]]
[[Súbor:ledschema.jpg|400px|thumb|center|Schéma zapojenia]]
Riadok 27: Riadok 27:
.
.
<tabs>
<tabs>
<tab name="AVR C-code"><source lang="c++" style="background: LightYellow;">
<tab name="Program"><source lang="c++" style="background: LightYellow;">
int main(void)
{


char ReceivedChar;
uart_init();
stdout = &mystdout;
uint16_t time = 0;
int m = 0;
while(1)
{
if(m==0){
printf("Menu\n 1.Nastavene efeky\n 2.Nastavit cely pas\n 3.Nastavit LEDky");
ReceivedChar = uart_getc();
m=1;
uart_puts("\033[2J");
uart_puts("\033[H");
}
//------------------------------------------Predpisane effekty----------------------------------------------------------------
if(ReceivedChar == '1')
{
//--------------------------------Effeky------------------------------------------
printf("Effekty\n 0.Vypnut\n 1.Red\n 2.Green\n 3.Blue\n 4.Rainbow-1\n 5.Rinbow-2\n 6.Breathing\n 7.Spat");
while(m==1)
{
ReceivedChar = uart_getc();
//----------------------RED-------------------
if ( ReceivedChar == '1' ) 
      {
  for (uint16_t i = 0; i < LED_COUNT; i++)
{
colors[i] = (rgb_color){ 100, 0, 0 };
}
led_strip_write(colors, LED_COUNT);
_delay_ms(20);        
  }
  //-------------------Red-------------------
                   
if (ReceivedChar == '2')                         
      {
      for (uint16_t i = 0; i < LED_COUNT; i++)
{
colors[i] = (rgb_color){ 0, 100, 0 };
}
led_strip_write(colors, LED_COUNT);
_delay_ms(20);   
  } 
 
    //-------------------------Blue-----------------------                           
if (ReceivedChar == '3')                         
      {
      for (uint16_t i = 0; i < LED_COUNT; i++)
{
colors[i] = (rgb_color){ 0, 0, 100 };
}
led_strip_write(colors, LED_COUNT);
_delay_ms(20);   
  }
 
  //---------------------------Vypnut------------------------
  if (ReceivedChar == '0')                         
      {
      for (uint16_t i = 0; i < LED_COUNT; i++)
{
colors[i] = (rgb_color){ 0, 0, 0 };
}
led_strip_write(colors, LED_COUNT);
_delay_ms(20);   
  }
 
  //--------------------------Rainbow--------------------
  if (ReceivedChar == '4')                         
      {
while (UDR0=='4')
{
for (uint16_t i = 0; i < LED_COUNT; i++)
{
uint8_t x = (time >> 2) - 8 * i;
colors[i] = (rgb_color){ x, 255-x, x };
}
led_strip_write(colors, LED_COUNT);
_delay_ms(20);
time += 20;
   
}
}
//-----------------------------Rainbow-----------------------------------
if (ReceivedChar == '5') {
    uint8_t hue = 1; // Initialize hue value
    while (UDR0 == '5') {
        for (uint16_t i = 0; i < LED_COUNT; i++) {
            // Calculate hue-modified colors
            colors[i] = hsv_to_rgb(hue);
            // Increment hue for the next LED
        }
hue += 1;
if(hue==255){hue=1;}
        // Write colors to LED strip
        led_strip_write(colors, LED_COUNT);
        // Delay for color transition
        _delay_ms(50);
    }
}
//----------------------------Breathing------------------------------------
  if (ReceivedChar == '6')                         
      {
  int c = 0;
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
while (UDR0=='6')
{
for (uint16_t i = 0; i < LED_COUNT; i++)
{
colors[i] = (rgb_color){ r, g, b };
}
if(c==0)
{
r++;
if(r==150)
{c=1;}
}
if(c==1)
{
r--;
if(r==0)
{c=0;}
}
led_strip_write(colors, LED_COUNT);
_delay_ms(20);   
}
}
//-------------------------Spat---------------------------------------
if (ReceivedChar == '7')                         
      {
m=0;
uart_puts("\033[2J");
uart_puts("\033[H");
}
}
//-------------------------------Samonastavenie celej pasy-------------------------------------------------------
if(ReceivedChar == '2')
{
char red[MAX_BUFFER_SIZE];
char green[MAX_BUFFER_SIZE];
char blue[MAX_BUFFER_SIZE];
while(m==1)
{
printf("RGB(0-255)-- Set blue to 300 to go back\n");
printf("Red brightness: ");
uart_gets(red, MAX_BUFFER_SIZE);
int num1 = atoi(red);
printf("%d \n",num1);
printf("Green brightness: ");
uart_gets(green, MAX_BUFFER_SIZE);
int num2 = atoi(green);
printf("%d \n",num2);
printf("Blue brightness: ");
uart_gets(blue, MAX_BUFFER_SIZE);
int num3 = atoi(blue);
printf("%d \n",num3);
if(num1>255 || num2>255 || num3>255)
{m=0;
uart_puts("\033[2J");
uart_puts("\033[H");
break;
}
for (uint16_t i = 0; i < LED_COUNT; i++)
{
colors[i] = (rgb_color){ num1, num2, num3 };
}
led_strip_write(colors, LED_COUNT);
_delay_ms(20);
uart_puts("\033[2J");
uart_puts("\033[H");
}
}
//-----------------------------------------------Mode 3------------------------------------------
if(ReceivedChar == '3')
{
char red[MAX_BUFFER_SIZE];
char green[MAX_BUFFER_SIZE];
char blue[MAX_BUFFER_SIZE];
char lednumber[MAX_BUFFER_SIZE];
while(m==1)
{
printf("CHoose the LED: -Set blue to 300 to go back\n");
uart_gets(lednumber, MAX_BUFFER_SIZE);
int led = atoi(lednumber);
printf("%d \n",led);
printf("Red brightness: ");
uart_gets(red, MAX_BUFFER_SIZE);
int num1 = atoi(red);
printf("%d \n",num1);
printf("Green brightness: ");
uart_gets(green, MAX_BUFFER_SIZE);
int num2 = atoi(green);
printf("%d \n",num2);
printf("Blue brightness: ");
uart_gets(blue, MAX_BUFFER_SIZE);
int num3 = atoi(blue);
printf("%d \n",num3);
if(num1>255 || num2>255 || num3>255)
{m=0;
uart_puts("\033[2J");
uart_puts("\033[H");
break;
}
colors[led] = (rgb_color){ num1, num2, num3 };
led_strip_write(colors, LED_COUNT);
_delay_ms(20);
uart_puts("\033[2J");
uart_puts("\033[H");
}
}  
}
return(0);
}
</source></tab>
<tab name="uart.c"><source lang="c++" style="background: LightYellow;">
#define F_CPU 16000000
#define BAUD 9600
#include <avr/io.h>
#include <util/setbaud.h>
void uart_init( void )
{
    UBRR0H = UBRRH_VALUE;
    UBRR0L = UBRRL_VALUE;
#if USE_2X
    UCSR0A |= _BV(U2X0);
#else
    UCSR0A &= ~(_BV(U2X0));
#endif
    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
    UCSR0B = _BV(RXEN0) | _BV(TXEN0);  /* Enable RX and TX */
}
//------------------------------------Uart Prikazy-------------------------------------------------
void uart_putc(char c)
{
  if (c == '\n')
    {
      uart_putc('\r');
    }
  loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
  UDR0 = c;
}
void uart_puts(const char *str) {
    // Transmit a null-terminated string via UART
    while (*str) {
        // Wait for the UART data register to be ready for transmission
        while (!(UCSR0A & (1 << UDRE0)));
        // Transmit the character
        UDR0 = *str++;
    }
}
char uart_getc(void) {
    loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
    return UDR0;
}
void uart_gets(char *buffer, uint8_t max_length) {
    uint8_t count = 0;
    char received_char;
    while (1) {
        received_char = uart_getc();
        // Check for newline or carriage return
        if (received_char == '\n' || received_char == '\r') {
            break;
        }
        // Append received character to the buffer
        if (count < max_length - 1) {
            buffer[count++] = received_char;
        }
    }
    // Null-terminate the buffer
    buffer[count] = '\0';
}
</source></tab>
<tab name="Hue"><source lang="c++" style="background: LightYellow;">
// Function to convert HSV to RGB
rgb_color hsv_to_rgb(uint8_t hue) {
    uint8_t sector = hue / 42; // Divide the hue into 6 sectors (42 degrees each)
    uint8_t remainder = (hue % 42) * 6; // Get the remainder and scale it to fit in a 8-bit range
    uint8_t red = 0, green = 0, blue = 0;
    switch (sector) {
        case 0:
            red = 255;
            green = remainder;
            break;
        case 1:
            red = 255 - remainder;
            green = 255;
            break;
        case 2:
            green = 255;
            blue = remainder;
            break;
        case 3:
            green = 255 - remainder;
            blue = 255;
            break;
        case 4:
            red = remainder;
            blue = 255;
            break;
        case 5:
            red = 255;
            blue = 255 - remainder;
            break;
    }
    rgb_color rgb = { red, green, blue };
    return rgb;
}




</source></tab>
</source></tab>
</tabs>
</tabs>


Pridajte sem aj zbalený kompletný projekt, napríklad takto (použite jednoznačné pomenovanie, nemôžeme mať na serveri 10x ''zdrojaky.zip'':
Zdrojový kód: [[Médiá:RGBLEDDominikIlles.c|RGBLED.c]]
 
Zdrojový kód: [[Médiá:projektMenoPriezvisko.zip|zdrojaky.zip]]




=== Overenie ===
=== Overenie ===


Na používanie našej aplikácie stačia dve tlačítka a postup používania je opísaný v sekcii popis riešenia.
Na konci uvádzame fotku záverečnej obrazovky pred resetom. Vypísaný je tu priemerný čas a najlepší čas.


[[Súbor:fotka.jpg|400px|thumb|center|Aplikácia.]]
[[Súbor:OverenieLEDpasa.jpg|400px|thumb|center|Aplikácia.]]


'''Video:'''
'''Video:'''

Verzia z 18:03, 15. máj 2024

Záverečný projekt predmetu MIPS / LS2023 - Dominik Illés


Zadanie

Ovládanie RGB led pásika pomocou jednoduchého menu v serial linke

Arduino Nano.

Literatúra:


Analýza a opis riešenia

Zapojenie LED pasíka je jednoduché len tri vodiče a v data vstupom som dal resistor, aby náhodne veľké prúdy nepoškodili pása. Najťažšia časť bola nastavid pás aby poslané farby vykresloval, na to som použil asembler kód čo som našiel na internete. Na ovládanie som napísal malú menu s tromi zvláštnymi módmi, používaním numpadu môžme navigovať cez módy prednastavené, manuálna nastavenia celého pása a manuálna nastavenia hocijakeho LEDa.

Schéma zapojenia


Algoritmus a program

.

int main(void)
{

	
	char ReceivedChar;

	uart_init();
	stdout = &mystdout;
	uint16_t time = 0;
	
	int m = 0;	
		
while(1)
	{
		if(m==0){
		printf("Menu\n 1.Nastavene efeky\n 2.Nastavit cely pas\n 3.Nastavit LEDky");
		ReceivedChar = uart_getc();
		m=1;
		uart_puts("\033[2J");
		uart_puts("\033[H");
		}
//------------------------------------------Predpisane effekty----------------------------------------------------------------
		if(ReceivedChar == '1')
		{
		//--------------------------------Effeky------------------------------------------
		printf("Effekty\n 0.Vypnut\n 1.Red\n 2.Green\n 3.Blue\n 4.Rainbow-1\n 5.Rinbow-2\n 6.Breathing\n 7.Spat");
		while(m==1)
		{
		ReceivedChar = uart_getc();
		
		//----------------------RED-------------------
		if ( ReceivedChar == '1' )   
       {
		  for (uint16_t i = 0; i < LED_COUNT; i++)
		{
		colors[i] = (rgb_color){ 100, 0, 0 };
		}

		led_strip_write(colors, LED_COUNT);

		_delay_ms(20);									         
	   }
	   //-------------------Red-------------------
	                     
		if (ReceivedChar == '2')                          
       {
		       for (uint16_t i = 0; i < LED_COUNT; i++)
		{
		 colors[i] = (rgb_color){ 0, 100, 0 };
		}

		led_strip_write(colors, LED_COUNT);

		_delay_ms(20);     
	   }  
	   
	    //-------------------------Blue-----------------------                            
		if (ReceivedChar == '3')                          
       {
		       for (uint16_t i = 0; i < LED_COUNT; i++)
		{
		 colors[i] = (rgb_color){ 0, 0, 100 };
		}

		led_strip_write(colors, LED_COUNT);

		_delay_ms(20);     
	   }
	   
	   //---------------------------Vypnut------------------------
	   	if (ReceivedChar == '0')                          
       {
		       for (uint16_t i = 0; i < LED_COUNT; i++)
		{
		 colors[i] = (rgb_color){ 0, 0, 0 };
		}

		led_strip_write(colors, LED_COUNT);

		_delay_ms(20);     
	   }
	   
	   //--------------------------Rainbow--------------------
	   if (ReceivedChar == '4')                          
       {
		 while (UDR0=='4')
		 {
		for (uint16_t i = 0; i < LED_COUNT; i++)
		{
		 uint8_t x = (time >> 2) - 8 * i;
		 colors[i] = (rgb_color){ x, 255-x, x };
		}

		led_strip_write(colors, LED_COUNT);

		_delay_ms(20);
		time += 20;
		     
			}
		}
		//-----------------------------Rainbow-----------------------------------
		
		
if (ReceivedChar == '5') {
    uint8_t hue = 1; // Initialize hue value
    while (UDR0 == '5') {
        for (uint16_t i = 0; i < LED_COUNT; i++) {
            // Calculate hue-modified colors
            colors[i] = hsv_to_rgb(hue);
            // Increment hue for the next LED

        }
		hue += 1;
		if(hue==255){hue=1;}
        // Write colors to LED strip
        led_strip_write(colors, LED_COUNT);
        // Delay for color transition
        _delay_ms(50);
    }
}
		//----------------------------Breathing------------------------------------
	   if (ReceivedChar == '6')                          
       {
		   int c = 0;
		 uint8_t r = 0;
		 uint8_t g = 0;
		 uint8_t b = 0;
		 while (UDR0=='6')
		 {
			for (uint16_t i = 0; i < LED_COUNT; i++)
			{
			colors[i] = (rgb_color){ r, g, b };
			}	 
					 if(c==0)
					 {
						r++;
						if(r==150)
						{c=1;}
					 }
					 if(c==1)
					 {
						 r--;
						 if(r==0)
						{c=0;}
					 }			
		led_strip_write(colors, LED_COUNT);
		_delay_ms(20);    
			}
		}
		
		//-------------------------Spat---------------------------------------				
		if (ReceivedChar == '7')                          
       {
		m=0;
		uart_puts("\033[2J");
		uart_puts("\033[H");
			 }		
		}   		
	}

//-------------------------------Samonastavenie celej pasy-------------------------------------------------------
		if(ReceivedChar == '2')
		{
			char red[MAX_BUFFER_SIZE];
			char green[MAX_BUFFER_SIZE];
			char blue[MAX_BUFFER_SIZE];
			while(m==1)
			{
				printf("RGB(0-255)-- Set blue to 300 to go back\n");
				printf("Red brightness: ");
				uart_gets(red, MAX_BUFFER_SIZE);
				int num1 = atoi(red);
				printf("%d \n",num1);
				printf("Green brightness: ");
				uart_gets(green, MAX_BUFFER_SIZE);
				int num2 = atoi(green);
				printf("%d \n",num2);
				printf("Blue brightness: ");
				uart_gets(blue, MAX_BUFFER_SIZE);
				int num3 = atoi(blue);
				printf("%d \n",num3);
				
				if(num1>255 || num2>255 || num3>255)
				{m=0;
				uart_puts("\033[2J");
				uart_puts("\033[H");
				break;
				}
				for (uint16_t i = 0; i < LED_COUNT; i++)
				{
					colors[i] = (rgb_color){ num1, num2, num3 };
				}

				led_strip_write(colors, LED_COUNT);

				_delay_ms(20);
				uart_puts("\033[2J");
				uart_puts("\033[H");	
				
				}				
			}
//-----------------------------------------------Mode 3------------------------------------------
		if(ReceivedChar == '3')
		{
			char red[MAX_BUFFER_SIZE];
			char green[MAX_BUFFER_SIZE];
			char blue[MAX_BUFFER_SIZE];
			char lednumber[MAX_BUFFER_SIZE];
			
			while(m==1)
			{
				printf("CHoose the LED: -Set blue to 300 to go back\n");
				uart_gets(lednumber, MAX_BUFFER_SIZE);
				int led = atoi(lednumber);
				printf("%d \n",led);				
				
				
				

				printf("Red brightness: ");
				uart_gets(red, MAX_BUFFER_SIZE);
				int num1 = atoi(red);
				printf("%d \n",num1);
				printf("Green brightness: ");
				uart_gets(green, MAX_BUFFER_SIZE);
				int num2 = atoi(green);
				printf("%d \n",num2);
				printf("Blue brightness: ");
				uart_gets(blue, MAX_BUFFER_SIZE);
				int num3 = atoi(blue);
				printf("%d \n",num3);
				
				if(num1>255 || num2>255 || num3>255)
				{m=0;
				uart_puts("\033[2J");
				uart_puts("\033[H");
				break;
				}
				colors[led] = (rgb_color){ num1, num2, num3 };

				led_strip_write(colors, LED_COUNT);

				_delay_ms(20);
				uart_puts("\033[2J");
				uart_puts("\033[H");	
								
				
			}

		}	   
	
}
	return(0);
}
#define F_CPU 16000000
#define BAUD 9600
#include <avr/io.h>
#include <util/setbaud.h>

void uart_init( void ) 
{
    UBRR0H = UBRRH_VALUE;
    UBRR0L = UBRRL_VALUE;

#if USE_2X
    UCSR0A |= _BV(U2X0);
#else
    UCSR0A &= ~(_BV(U2X0));
#endif

    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
    UCSR0B = _BV(RXEN0) | _BV(TXEN0);   /* Enable RX and TX */
}

//------------------------------------Uart Prikazy-------------------------------------------------
void uart_putc(char c) 
{
   if (c == '\n') 
    {
       uart_putc('\r');
    }
   loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
   UDR0 = c;
}

void uart_puts(const char *str) {
    // Transmit a null-terminated string via UART
    while (*str) {
        // Wait for the UART data register to be ready for transmission
        while (!(UCSR0A & (1 << UDRE0)));
        // Transmit the character
        UDR0 = *str++;
    }
}

char uart_getc(void) {
    loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
    return UDR0;
}

void uart_gets(char *buffer, uint8_t max_length) {
    uint8_t count = 0;
    char received_char;

    while (1) {
        received_char = uart_getc();

        // Check for newline or carriage return
        if (received_char == '\n' || received_char == '\r') {
            break;
        }

        // Append received character to the buffer
        if (count < max_length - 1) {
            buffer[count++] = received_char;
        }
    }

    // Null-terminate the buffer
    buffer[count] = '\0';
}
// Function to convert HSV to RGB
rgb_color hsv_to_rgb(uint8_t hue) {
    uint8_t sector = hue / 42; // Divide the hue into 6 sectors (42 degrees each)
    uint8_t remainder = (hue % 42) * 6; // Get the remainder and scale it to fit in a 8-bit range

    uint8_t red = 0, green = 0, blue = 0;

    switch (sector) {
        case 0:
            red = 255;
            green = remainder;
            break;
        case 1:
            red = 255 - remainder;
            green = 255;
            break;
        case 2:
            green = 255;
            blue = remainder;
            break;
        case 3:
            green = 255 - remainder;
            blue = 255;
            break;
        case 4:
            red = remainder;
            blue = 255;
            break;
        case 5:
            red = 255;
            blue = 255 - remainder;
            break;
    }

    rgb_color rgb = { red, green, blue };
    return rgb;
}


Zdrojový kód: RGBLED.c


Overenie

Aplikácia.

Video:

Kľúčové slová 'Category', ktoré sú na konci stránky nemeňte.