Monday 17 October 2022

Control Arduino Using GUI (Arduino + Processing)

https://www.hackster.io/hardikrathod/control-arduino-using-gui-arduino-processing-2c9c6c






Arduino Sketch

void setup() {

  pinMode(10, OUTPUT);   //set pin as output , blue led
  pinMode(11, OUTPUT);   //set pin as output , red led
  pinMode(12, OUTPUT);   //set pin as output , yellow led

  Serial.begin(9600);    //start serial communication @9600 bps
  }

void loop(){
  
  if(Serial.available()){  //id data is available to read

    char val = Serial.read();

    if(val == 'r'){       //if r received
      digitalWrite(11, HIGH); //turn on red led
      }
    if(val == 'b'){       //if b received
      digitalWrite(10, HIGH); //turn on blue led
      }
    if(val == 'y'){       //if y received
      digitalWrite(12, HIGH); //turn on yellow led
      }
    if(val == 'f'){       //if f received
      digitalWrite(11, LOW); //turn off all led
      digitalWrite(12, LOW);
      digitalWrite(10, LOW);
      }      
    }
  }


Processing IDE Sketch





import controlP5.*; //import ControlP5 library import processing.serial.*; Serial port; ControlP5 cp5; //create ControlP5 object PFont font; void setup(){ //same as arduino program size(300, 450); //window size, (width, height) printArray(Serial.list()); //prints all available serial ports port = new Serial(this, "COM3", 9600); //i have connected arduino to com3, it would be different in linux and mac os //lets add buton to empty window cp5 = new ControlP5(this); font = createFont("calibri light bold", 20); // custom fonts for buttons and title cp5.addButton("red") //"red" is the name of button .setPosition(100, 50) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; cp5.addButton("yellow") //"yellow" is the name of button .setPosition(100, 150) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; cp5.addButton("blue") //"blue" is the name of button .setPosition(100, 250) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; cp5.addButton("alloff") //"alloff" is the name of button .setPosition(100, 350) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; } void draw(){ //same as loop in arduino background(150, 0 , 150); // background color of window (r, g, b) or (0 to 255) //lets give title to our window fill(0, 255, 0); //text color (r, g, b) textFont(font); text("LED CONTROL", 80, 30); // ("text", x coordinate, y coordinat) } //lets add some functions to our buttons //so whe you press any button, it sends perticular char over serial port void red(){ port.write('r'); } void yellow(){ port.write('y'); } void blue(){ port.write('b'); } void alloff(){ port.write('f'); }




Processing IDEimport controlP5.*; //import ControlP5 library import processing.serial.*; Serial port; ControlP5 cp5; //create ControlP5 object PFont font; void setup(){ //same as arduino program size(300, 450); //window size, (width, height) printArray(Serial.list()); //prints all available serial ports port = new Serial(this, "COM3", 9600); //i have connected arduino to com3, it would be different in linux and mac os //lets add buton to empty window cp5 = new ControlP5(this); font = createFont("calibri light bold", 20); // custom fonts for buttons and title cp5.addButton("red") //"red" is the name of button .setPosition(100, 50) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; cp5.addButton("yellow") //"yellow" is the name of button .setPosition(100, 150) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; cp5.addButton("blue") //"blue" is the name of button .setPosition(100, 250) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; cp5.addButton("alloff") //"alloff" is the name of button .setPosition(100, 350) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ; } void draw(){ //same as loop in arduino background(150, 0 , 150); // background color of window (r, g, b) or (0 to 255) //lets give title to our window fill(0, 255, 0); //text color (r, g, b) textFont(font); text("LED CONTROL", 80, 30); // ("text", x coordinate, y coordinat) } //lets add some functions to our buttons //so whe you press any button, it sends perticular char over serial port void red(){ port.write('r'); } void yellow(){ port.write('y'); } void blue(){ port.write('b'); } void alloff(){ port.write('f'); }




Saturday 15 October 2022

Arduino AM Transmitter

 

 https://www.instructables.com/AM-Transmitter-With-Arduino/

 https://www.youtube.com/watch?v=Xy7DiPVi18Y

 

 

/*
 Name:        transmitter.ino
 Created:    11/13/2019 7:48:39 PM
 Author:    michael
*/

#define ANTENNA_PIN PB3 //Arduino Nano/Uno D11

// the setup function runs once when you press reset or power the board
void setup() {
    Serial.begin(115200);

    //Carrier Frequency generation
    uint32_t fTransmit = 600; //KHz
    DDRB |= (1 << ANTENNA_PIN);
    TCCR2A = (0 << COM2A1) + (1 << COM2A0); //Toggle OC0A on Compare Match
    TCCR2A |= (1 << WGM21) + (0 << WGM20); //CTC
    TCCR2B = (0 << CS22) + (0 << CS21) + (1 << CS20); //No Prescaling
    OCR2A = F_CPU / (2000 * fTransmit) - 1;
    
    char strbuf[255];
    sprintf(strbuf, "Will broadcast at %d KHz", (F_CPU / (2 * (1 + OCR2A)) / 1000));
    Serial.println(strbuf);

    //PWM Signal generation
    //TCCR1A |= (0 << WGM11) + (1 << WGM10);  //Fast PWM 8 Bit
    //TCCR1A |= (1 << WGM11) + (0 << WGM10);  //Fast PWM 9 Bit
    TCCR1A |= (1 << WGM11) + (1 << WGM10); //Fast PWM 10 Bit
    TCCR1B = (1 << WGM12);                   
    TCCR1B |= (0 << CS12) + (0 << CS11) + (1 << CS10); //No Prescaling
    TIMSK1 = (1 << OCIE1A) + (1 << TOIE1);

    //ADC Settings
    ADMUX = (1 << REFS1) + (1 << REFS0); //Reference internal 1.1V
    ADCSRA = (1 << ADEN) + (1 << ADSC) + (1 << ADATE); //Auto Trigger enable, free running
    ADCSRA |= (1 << ADPS2) + (1 << ADPS1) + (0 << ADPS0); //Divide by 64 -> 18.5k Sample rate
    DIDR0 = (1 << ADC0D);
}

ISR(TIMER1_OVF_vect) {
    uint8_t adcl = ADCL;
    uint8_t adch = ADCH;
    OCR1A = (adch << 8) + adcl;
    DDRB |= (1 << ANTENNA_PIN);
}

ISR(TIMER1_COMPA_vect) {
    DDRB &= ~(1 << ANTENNA_PIN);
}

void loop() {
}