Operácie

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

Z SensorWiki

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, používaním numpadu môžme navigovať cez módy prednastavené, manuálna nastavenia celého pása a manuálna nastavenia hocijakeho LEDa.
+
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. Aby nevykreslilo vždy pod sebou text a vyzeralo lepšie som použil printf("\033[2J"); čo vymaže čo je na terminále a printf("\033[H"); aby kurzor bolo na začiatku.
 +
 
  
 
[[Súbor:ledschema.jpg|400px|thumb|center|Schéma zapojenia]]
 
[[Súbor:ledschema.jpg|400px|thumb|center|Schéma zapojenia]]
Riadok 30: Riadok 31:
 
int main(void)
 
int main(void)
 
{
 
{
 
 
 
char ReceivedChar;
 
char ReceivedChar;
 
 
uart_init();
 
uart_init();
 
stdout = &mystdout;
 
stdout = &mystdout;
 
uint16_t time = 0;
 
uint16_t time = 0;
 
 
int m = 0;
 
int m = 0;
 
 
Riadok 46: Riadok 43:
 
ReceivedChar = uart_getc();
 
ReceivedChar = uart_getc();
 
m=1;
 
m=1;
uart_puts("\033[2J");
+
printf("\033[2J");
uart_puts("\033[H");
+
printf("\033[H");
 
}
 
}
//------------------------------------------Predpisane effekty----------------------------------------------------------------
+
//------------------------------------Predpisane effekty----------------------------------------------------------------
 
if(ReceivedChar == '1')
 
if(ReceivedChar == '1')
 
{
 
{
//--------------------------------Effeky------------------------------------------
+
//--------------------------------Effekty-----------------------------
 
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");
 
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)
 
while(m==1)
 
{
 
{
ReceivedChar = uart_getc();
+
ReceivedChar = uart_getc();
+
//------------------------------RED---------------------------------
//----------------------RED-------------------
 
 
if ( ReceivedChar == '1' )   
 
if ( ReceivedChar == '1' )   
 
       {
 
       {
  for (uint16_t i = 0; i < LED_COUNT; i++)
+
RedStrip();        
{
 
colors[i] = (rgb_color){ 100, 0, 0 };
 
}
 
 
 
led_strip_write(colors, LED_COUNT);
 
 
 
_delay_ms(20);        
 
 
  }
 
  }
  //-------------------Red-------------------
+
  //------------------------------Green---------------------------------                    
                   
 
 
if (ReceivedChar == '2')                           
 
if (ReceivedChar == '2')                           
 
       {
 
       {
      for (uint16_t i = 0; i < LED_COUNT; i++)
+
GreenStrip();
{
+
  }     
colors[i] = (rgb_color){ 0, 100, 0 };
+
    //------------------------------Blue--------------------------------                             
}
 
 
 
led_strip_write(colors, LED_COUNT);
 
 
 
_delay_ms(20);      
 
  } 
 
 
 
    //-------------------------Blue-----------------------                             
 
 
if (ReceivedChar == '3')                           
 
if (ReceivedChar == '3')                           
 
       {
 
       {
      for (uint16_t i = 0; i < LED_COUNT; i++)
+
BlueStrip();
{
+
  }  
colors[i] = (rgb_color){ 0, 0, 100 };
+
  //------------------------------Vypnut--------------------------------
}
 
 
 
led_strip_write(colors, LED_COUNT);
 
 
 
_delay_ms(20);    
 
  }
 
 
 
  //---------------------------Vypnut------------------------
 
 
  if (ReceivedChar == '0')                           
 
  if (ReceivedChar == '0')                           
 
       {
 
       {
      for (uint16_t i = 0; i < LED_COUNT; i++)
+
OffStrip();
{
 
colors[i] = (rgb_color){ 0, 0, 0 };
 
}
 
 
 
led_strip_write(colors, LED_COUNT);
 
 
 
_delay_ms(20);    
 
 
  }
 
  }
 
+
//------------------------------------RainbowV1---------------------------------
  //--------------------------Rainbow--------------------
 
 
  if (ReceivedChar == '4')                           
 
  if (ReceivedChar == '4')                           
 
       {
 
       {
while (UDR0=='4')
+
Rainbow1();
{
+
//-------------------------------------Rainbow-----------------------------------
for (uint16_t i = 0; i < LED_COUNT; i++)
+
  }
{
+
if (ReceivedChar == '5') {
uint8_t x = (time >> 2) - 8 * i;
+
Rainbow2();
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------------------------------------
 
//----------------------------Breathing------------------------------------
 
  if (ReceivedChar == '6')                           
 
  if (ReceivedChar == '6')                           
 
       {
 
       {
  int c = 0;
+
Breathing();
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---------------------------------------
//-------------------------Spat---------------------------------------
 
 
if (ReceivedChar == '7')                           
 
if (ReceivedChar == '7')                           
 
       {
 
       {
 
m=0;
 
m=0;
uart_puts("\033[2J");
+
printf("\033[2J");
uart_puts("\033[H");
+
printf("\033[H");
 
}
 
}
 
 
 
}
 
}
 
+
//-------------------------------Nastavenie celej pasy-------------------------------------------------------
//-------------------------------Samonastavenie celej pasy-------------------------------------------------------
 
 
if(ReceivedChar == '2')
 
if(ReceivedChar == '2')
 
{
 
{
char red[MAX_BUFFER_SIZE];
+
fullLED(&m);
char green[MAX_BUFFER_SIZE];
+
}
char blue[MAX_BUFFER_SIZE];
+
//-------------------------------Nastavenie zvlast ledky----------------------------------------------------------
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')
 
if(ReceivedChar == '3')
 
{
 
{
char red[MAX_BUFFER_SIZE];
+
pieceLED(&m);
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);
 
return(0);
 
}
 
}
Riadok 398: Riadok 227:
 
   
 
   
  
Zdrojový kód: [[Médiá:RGBLEDDominikIlles.c|RGBLED.c]]
+
Zdrojový kód: [[Médiá:LEDovlad.c|LEDovlad.c]]
  
  

Verzia zo dňa a času 17:22, 17. 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. Aby nevykreslilo vždy pod sebou text a vyzeralo lepšie som použil printf("\033[2J"); čo vymaže čo je na terminále a printf("\033[H"); aby kurzor bolo na začiatku.


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;
		printf("\033[2J");
		printf("\033[H");
		}
//------------------------------------Predpisane effekty----------------------------------------------------------------
		if(ReceivedChar == '1')
		{
		//--------------------------------Effekty-----------------------------
		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' )   
       {
			RedStrip();						         
	   }
	   //------------------------------Green---------------------------------	                     
		if (ReceivedChar == '2')                          
       {
			GreenStrip();
	   }     
	    //------------------------------Blue--------------------------------                            
		if (ReceivedChar == '3')                          
       {
			BlueStrip();
	   }   
	   //------------------------------Vypnut--------------------------------
	   	if (ReceivedChar == '0')                          
       {
			OffStrip();
	   }
//------------------------------------RainbowV1---------------------------------
	   if (ReceivedChar == '4')                          
       {
		Rainbow1();
//-------------------------------------Rainbow-----------------------------------	
	   }		
	if (ReceivedChar == '5') {
		Rainbow2();
	}
		//----------------------------Breathing------------------------------------
	   if (ReceivedChar == '6')                          
       {
		Breathing();
		}
//---------------------------------------Spat---------------------------------------				
		if (ReceivedChar == '7')                          
       {
		m=0;
		printf("\033[2J");
		printf("\033[H");
			 }		
		}   		
	}
//-------------------------------Nastavenie celej pasy-------------------------------------------------------
		if(ReceivedChar == '2')
		{
			fullLED(&m);		
		}
//-------------------------------Nastavenie zvlast ledky----------------------------------------------------------
		if(ReceivedChar == '3')
		{
			pieceLED(&m);
		}	   
	}

	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: LEDovlad.c


Overenie

Aplikácia.

Video:

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