Arduino Basic (PWM)


Exercise 1a

int x=0;
void setup()
{
Serial.begin(9600);
Serial.println("Initializing...");
delay(100);
}
void loop()
{
 Serial.print("This is pwm_value: "); 
 Serial.println(x);
 x=x+1;
 delay(1000);       
}

Exercise 2a

void setup()

{
  Serial.begin(9600);
}
void loop()
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.print("sensorValue=");
Serial.print(sensorValue);
Serial.print(" voltage=");
Serial.println(voltage);
delay(100);
}


Exercise 3a

int pwmPin = 9;
int potPin = A0;

void setup() 
{
pinMode(pwmPin, OUTPUT);    
pinMode(potPin, INPUT);    
Serial.begin(9600);
}
void loop() 
int value  =  analogRead(potPin);   
int duty = value/4;             
analogWrite(pwmPin, duty);        
Serial.print(value);                
Serial.print("\t"); 
Serial.println(duty);       
delay(100);  
}








Arduino Workshop (Basic Level)


Exercise 1

int buttonPin = 9;   //pin assignment for button 
int ledPin = 8; //pin assignment for led 
void setup(){ 
Serial.begin(9600);         // initialize serial communication at 9600 
pinMode(ledPinOUTPUT);      // Use pin 8 for digital output
pinMode(buttonPinINPUT_PULLUP); // Use pin 9 for digital input 
} 
void loop(){
boolean state;       // state either 1 or 0 
state  =  digitalRead(buttonPin);  // read state of pin 9
digitalWrite(ledPin state);  // set state of pin 8 (LED)
Serial.println(state);       // print out the state 
delay(100);  // wait for a 100 millisecond 
}

Exercise 2a

// Convert the analog reading  which goes from (0 - 1023) to a voltage (0 - 5V)

void setup() 
{Serial.begin(9600);  //initialize serial communication at 9600 bits per second:
}
void loop() 
{
  
int  sensorValue = analogRead(A0);    // read the input on analog pin 0:
  
float voltage = sensorValue * (5.0 / 1023.0);   
  Serial.print(“analog=“);   
  Serial.print(sensorValue);     // print out the analog value you read:

  Serial.print(“ voltage=“);   
  Serial.println(voltage);     // print out the corresponding voltage
}

Exercise 2b

int ledPin = 9;
int potPin = A0;
void setup()
{
pinMode(ledPinOUTPUT);    // Use pin for analog output
pinMode(potPinINPUT);    // Use pin 0 for analog input
Serial.begin(9600);         // initialize serial communication at 9600
}
void loop()
{
int value  =  analogRead(potPin );  // read analog of pin A0
digitalWrite(ledPinHIGH);            // turn on led
delay(value);  // turn on time delay
digitalWrite(ledPinLOW); // turn off led
delay(value);  // turn off time delay
Serial.println(value);       // print out the delay time in milliseconds

}

Exercise 2c

int ledPin = 9;
int potPin = A0;
void setup()
{
pinMode(ledPinOUTPUT);    // Use pin for analog output
pinMode(potPinINPUT);    // Use pin 0 for analog input
Serial.begin(9600);         // initialize serial communication at 9600
// Use pin 9 as PWM analog output, PWM pins require no pin assign
}
void loop()
{
int value  =  analogRead(potPin);   // read analog of pin A0
int duty = value/4;             // convert the value to percentage
analogWrite(ledPin, duty);        // output PWM signal at pin 9
Serial.print(value);                // print out the value
Serial.print(“\t”);                // create spacebar
Serial.println(duty);       // print out the duty
delay(100);  // wait for a 100 millisecond
}

Exercise 2d

int photocellPin = 0// the cell and 10K pulldown are connected to A0
int photocellReading//the analog reading from the analog resistor divider
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading
if (photocellReading < 10) {
Serial.println(" - Dark");
else if (photocellReading < 200) {
Serial.println(" - Dim");
else if (photocellReading < 500) {
Serial.println(" - Light");
else if (photocellReading < 800) {
Serial.println(" - Bright");
else {
Serial.println(" - Very bright");
}
delay(1000);

}

Exercise 2e

int photocellPin = 0; // the cell and 10K pulldown are connected to A0
int photocellReading; //the analog reading from the analog resistor divider
float voltage; //the value
int LEDpin = 11;
int LEDbrightness;
void setup(void)
{
Serial.begin(9600);
}
void loop(void)
{
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
voltage= (photocellReading*5)/1023;
Serial.print("voltage = ");
Serial.println(voltage); // the raw analog reading
//photocellReading = 1023 - photocellReading;
LEDbrightness = map(voltage, 4, 1, 0, 255);
analogWrite(LEDpin, LEDbrightness);
delay(100);
}


Exercise 3a

void setup()
{
Serial.begin(9600);  // initialize serial communication at 9600 bits per second
Serial.println(“Text Display");   // print out the value you read:
delay(100); 
}
void loop()
{
 Serial.print(“My Name is:");   // print out the value you read:
 delay(1000);        // delay in between reads for stability
 Serial.print(“\t");   // tab the print
 Serial.println(“Put Your Name Here");   // print out the value you read:
 delay(1000);        // delay in between reads for stability

}


Exercise 3b

char incomingByte=0; 
void setup()
{
 Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
 Serial.println("Convert ASCII Char to others");
 delay(100);
}
void loop()  {
  if (Serial.available() > 0)
  {
   incomingByte = Serial.read();   // read the latest byte:
   Serial.print("ASCII Char:");
   Serial.println(incomingByte);   //print out the byte in Ascii Character
   Serial.print("HEX=");
   Serial.println(incomingByte,HEX);    //print out the byte in HEX
   Serial.print("Decimal=");
   Serial.println(incomingByte,DEC);  //print out the byte in decimal
   Serial.print("Binary=");
   Serial.println(incomingByte,BIN);  //print out the byte in binary
   Serial.print("\n");        //New line 
  }  
  delay(200);

}

Exercise 3c

int c;
int duty = 0;
int ledPin = 9;
void setup()
{
Serial.begin(9600);
Serial.setTimeout(10); // Serial.begin data read faster
}
void loop()
{
  if (Serial.available())
   {
   Serial.parseInt();
   }
 Serial.print(c);
 duty = map(c, 0, 9, 0, 255);   //mapping number 0 - 9 to 0-255
 analogWrite(ledPinduty);       // output PWM signal at pin 9
 Serial.print("\t");                // create spacebar
 Serial.println(duty);       // print out the duty
 delay(100);                    // wait for 100 millisecond
}


Tugasan 1

int GREEN = 2;
int YELLOW = 3;
int RED = 4;
int DELAY_GREEN = 5000;
int DELAY_YELLOW = 2000;
int DELAY_RED = 5000;
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT); }

void loop()
{
green_light();
delay(DELAY_GREEN);
yellow_light();
delay(DELAY_YELLOW);
red_light();
delay(DELAY_RED); }
void green_light() {
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW); }
void yellow_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW); }
void red_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}


Online Tutorial 1

Online Tutorial 2