/Users/michael/Documents/Venturers/JOTA:JOTI/Circuits/Woggle/main.c
/* 
 * File:   newmain.c
 * Author: Michael Gross
 * 1st Cobargo Scout group
 * www.cobargoscouts.org.au
 *
 * Version 1
 * 
 * Created on 11th March, 2025, 10:44 AM
 */


// PIC12F1572 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = INTOSC    //  (INTOSC oscillator; I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOREN = OFF    // Low Power Brown-out Reset enable bit (LPBOR is disabled)
#pragma config DEBUG = OFF      // In-Circuit Debugger Mode (In-Circuit Debugger disabled, ICSPCLK and ICSPDAT are general purpose I/O pins)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

#define _XTAL_FREQ 2000L  //500000L original value 

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#include <stdio.h>
#include <stdlib.h>

//Define Variables
    signed short i; //used in loops
    signed short n; //used in loops
    signed short t; //used in loops
    signed short p; //used in loops
    signed short pattern_speed; //used in loops as the number of repetitions so that multiple leds appear to be on continuously, and used to change pattern speed..
    short int y; //used in loops
    short int cycle; //used to define the number of times a led pattern loop is run
    short int random_led_select[]={8,3,6,1,5,7,4,2,1,5,2,7,3,5,6,8,4,7,5,1};
    short int crossover_led_select[]={3,5,1,7,2,8,6,4,2,8,1,7,3,5,5,7,3,2,4,1,6,8,4,1,2,3,5,7,7,2,5,4,3,6,1,8,3,6,5,4,7,2,2,4,7,6,8,5,3,1,8,5,6,7,2,4,4,6,2,8,7,1,3,5,7,1,2,8,4,6};
    short int running_led[]={2,6,1,5,4,8,3,7,6,1,5,2};
    short int clock_led[]={5,7,2,4,6,8,1,3};
    
//Case function to set port to inputs/ouputs and relevant ports high/low so that one led can be light at a time

void ledmap(unsigned short num){       //Led IO maps for TRIS and +ve pin needed to turn lED on, All out put pins are set to 0 with LATA
      switch(num){
        case 1: TRISA  = 0b00111001;RA2=0;RA1=1;break; //LED 1 only pins RA2 (-ve pin) and RA1 (+ve pin) to be an output
        case 2: TRISA  = 0b00011011;RA2=0;RA5=1;break; //LED 2 only pins RA2 (-ve pin) and RA5 (+ve pin) to be an output
        case 3: TRISA =  0b00001111;RA5=0;RA4=1;break; //LED 3 only pins RA4 (+ve pin) and RA5 (-ve pin) to be an output
        case 4: TRISA  = 0b00001111;RA4=0;RA5=1;break; //LED 4 only pins RA4 (+ve pin) and RA5 (-ve pin) to be an output
        case 5: TRISA  = 0b00011101;RA5=0;RA1=1;break; //LED 5 only pins RA1 (+ve pin) and RA5 (-ve pin) to be an output
        case 6: TRISA  = 0b00101101;RA4=0;RA1=1;break;  //LED 6 only pins RA1 (+ve pin) and RA4 (-ve pin) to be an output
        case 7: TRISA  = 0b00011011;RA5=0;RA2=1;break; //LED 7 only pins RA2 (+ve pin) and RA5 (-ve pin) to be an output
        case 8: TRISA  = 0b00101011;RA4=0;RA2=1;break; //LED 8 only pins RA2 (+ve pin) and RA4 (-ve pin) to be an output
 
    } 
}    


 void Pattern_1 (void) {   // Turn on random single leds one at a time.
          
    
 LATA  = 0b00000000; // all port bits off
         
            for (t=0; t<=19; t++) {   
               n= random_led_select[t];
               ledmap(n);
                __delay_ms(200000);
            }
 }




//Pattern 2, opposing same coloured pairs rotating clockwise

void Pattern_2 () {
    
 LATA   = 0b00000000; // all port bits off
 
            i=-1; //Starting count add 2 during loop to select odd numbers

        while(i<6) {    //Select Leds in pairs, 6 pairs in total
             i+=2;      //1st loop starts at Led 1 and increments in 2's
      
            for (t=0; t<=pattern_speed; t++) {   //pattern_speed = the number of cycles between leds, passed from "Main"
                n=i;
                ledmap(n);      //Turn on the 1st Led in the pair
                __delay_ms(100);    //Delay to ensure led is bright enough
                n++;            //Variable to select next led in the pair.
                ledmap(n);      //Turn on the 2nd Led in the pair
            }
        }
}


//Pattern 3, opposing same coloured pairs rotating anti clockwise
void Pattern_3 () {
    
 LATA   = 0b00000000; // all port bits off
 
            i=9; //Starting count

        while(i>1) {    //Select Leds in pairs
             i-=2;      //1st loop starts at Led 1 and increments in 2's
      
            for (t=0; t<=pattern_speed; t++) {   //pattern_speed = the number of cycles between leds, passed from "Main"
                n=i;
                ledmap(n);      //Turn on the 1st Led in the pair
                __delay_ms(100);    //Delay to ensure led is bright enough
                n++;            //Variable to select next led in the pair.
                ledmap(n);      //Turn on the 2nd Led in the pair
            }
              
        }
}


//Pattern 4, 2 single leds running in opposite directions, and crossing over
void Pattern_4 () {
     
  
         i=-2; //Starting count add 2 during loop to select start of next pair

        while(i<70) {    //Select Leds in pairs, A "For" loop at this stage added unnecessary processing delay that stopped Leds cycling smoothly after each sequence
             i+=2;      //1st loop starts at crossover_led_select (0);)
 
                for (t=0; t<=pattern_speed; t++) {   //pattern_speed = the speed of cycles between leds, passed from "Main"
                    LATA   = 0b00000000; // all port bits off
                    n=crossover_led_select[i]; //select led to turn on from array
                    ledmap(n);      //Turn on the 1st Led in the pair
                    __delay_ms(100);    //Delay to ensure led is bright enough
                    LATA   = 0b00000000; // all port bits off
                    n=crossover_led_select[i+1];    //select next led to turn on from array
                    ledmap(n);      //Turn on the 2nd Led in the pair
                }

        }        
}

//Pattern 5, Chasing leds, 4 on at a time rotating
void Pattern_5 () {
        i=-4; //Starting count add 4 during loop to select start of next 4 leds

        while(i<3) {    //Select Leds in pairs, A "For" loop at this stage added unnecessary processing delay that stopped Leds cycling smoothly after each sequence
             i+=4;      //1st loop starts at crossover_led_select (0);)
 
                for (t=0; t<=pattern_speed; t++) {   //pattern_speed = the speed of cycles between leds, passed from "Main"
                    LATA   = 0b00000000; // all port bits off
                    n=running_led[i]; //select led to turn on from array
                    ledmap(n);      //Turn on the 1st Led in the pair
                    __delay_ms(100);    //Delay to ensure led is bright enough
                    LATA   = 0b00000000; // all port bits off
                    n=running_led[i+1];    //select next led to turn on from array
                    ledmap(n);      //Turn on the 2nd Led in the pair
                    LATA   = 0b00000000; // all port bits off
                    n=running_led[i+2]; //select led to turn on from array
                    ledmap(n);      //Turn on the 3rd Led in the pair
                    __delay_ms(100);    //Delay to ensure led is bright enough
                    LATA   = 0b00000000; // all port bits off
                    n=running_led[i+3];    //select next led to turn on from array
                    ledmap(n);      //Turn on the 4th Led
                    
                }
             }        
    }

  //Pattern 6, Clock pattern
    void Pattern_6 () {
        i=-1; //  i is the minutes hand LED
            
        for (p=0; p<7; p++) {   //p is the number of the hours hand LEd
            
            if (i=7) { i=-1;    // if the minutes hand LED has got to the end , reset it
            }
            
            
             while(i<7) {//Select Leds in pairs, A "For" loop at this stage added unnecessary processing delay that stopped the Leds cycling smoothly after each sequence
                i++; 
                for (t=0; t<=pattern_speed; t++) {   //pattern_speed = the speed of cycles between leds, passed from "Main"
                    LATA   = 0b00000000; // all port bits off
                    n=clock_led[i]; //select Minutes led to turn on from array
                    ledmap(n);      //Turn on the 1st Led 
                    __delay_ms(100);    //Delay to ensure led is bright enough
                    n=clock_led[p]; //select hours LED to turn on from array
                    ledmap(n);      //Turn on the 2nd Led                   
                    __delay_ms(100);    //Delay to ensure led is bright enough  
                }

              }  
                            
            }
    }
                
               
//Pattern 7, Leapfrog, the LEDS rotate clockwise, looking like they leap over each other
    void Pattern_7 () {
        i=-1;   // i is the 1st LED
        p=0;    // p is the 2nd LED
            
            while(i<7) {//Select Leds in pairs, A "For" loop at this stage added unnecessary processing delay that stopped Leds cycling smoothly after each sequence
                i++; 
                if (i==p){p++;
                }
                if (i==7) {(p=0);}
               
                for (t=0; t<=pattern_speed; t++) {   //pattern_speed = the speed of cycles between leds, passed from "Main"
                    LATA   = 0b00000000; // all port bits off
                    n=clock_led[i]; //select led to turn on from array
                    ledmap(n);      //Turn on the 1st Led 
                    __delay_ms(100);    //Delay to ensure led is bright enough
                    n=clock_led[p]; //select led to turn on from array
                    ledmap(n);      //Turn on the 2nd Led                   
                    __delay_ms(100);    //Delay to ensure led is bright enough  
                }
                
            }
    }            
        
        


    
void main(void) {
    OSCCON = 0b01010000; // 1010 = 500kHz MF
    ANSELA = 0b00000000; // all pins to digital
    LATA   = 0b00000000; // all port bits off
    TRISA  = 0b00001000; // all pins to outputs except RA3(MCLR)
 
   while(1) {
   
 
      //Pattern 1 - Random leds illuminated
            cycle=1;
        
            while(cycle>0)  {
                Pattern_1();
                cycle--;     
            }
        
 
  
     //Pattern 2  Slow start pairs rotating clockwise, then speed up to all leds on.
            cycle=50;
            pattern_speed=200;
             
            while (cycle>0){
                Pattern_2() ;
    
                if (pattern_speed>12) {   
                pattern_speed-=10;          
            } 
            
            LATA   = 0b00000000; // all port bits off
            cycle--;
        }
 
   
   
     //Pattern 3  Fast start, leds rotating anticlockwise then slow down
        cycle=22;
        pattern_speed=10;
        
        while (cycle>0){
            Pattern_3() ;
    
           if (pattern_speed<200) {   
            pattern_speed+=10;          
         }  
            
           LATA   = 0b00000000; // all port bits off
           cycle--;
        } 
        
  
   
 
 
   //Pattern 4 - 2 single leds running in opposite directions, and crossing over
     
       pattern_speed=100;
       cycle=5;
        
            while(cycle>0)  {
                Pattern_4();               
                cycle--;         
            }
          

    
        //Pattern 5 - 4 leds running clockwise
     
       pattern_speed=100;
       cycle=30;
        
            while(cycle>0)  {
                Pattern_5();               
                cycle--;         
            }

  
         //Pattern 6 - clock pattern
     
       pattern_speed=100;
       cycle=2;

        
            while(cycle>0)  {
                Pattern_6();               
                cycle--;         
            }
       
   
       //Pattern 7 - leapfrog pattern
     
       pattern_speed=200;
       cycle=6;

        
            while(cycle>0)  {
                Pattern_7();               
                cycle--;         
            }
   }
}