Scratch Arduino Programs for ASER Lessons

These images should serve as a guide for what scratch programs should look like for ASER lessons 3-5.  I will update this page with formatting to look more presentable in the future.

Lesson 3: Arudino LED blinker

The Arduino code:

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

The Scratch Equivalent:




Lesson 4: Arduino LED Button

The Arduino Code:
int pushButton = 2;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  Serial.println(buttonState);
  delay(1);       
}

The Scratch Equivalent:


Lesson 5: Arduino LED Passcode Pad

Using a button to turn on an LED

Arduino Code:

int pushButton = 2;
int led = 13;


void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
  pinMode(led, OUTPUT);

}

void loop() {
  int buttonState = digitalRead(pushButton);
  digitalWrite(led1,buttonState);
  Serial.println(buttonState);
  delay(1);        
}

Scratch Equivalent:

NOTE: This uses an if/else statement to function, which should be explained at this point.

Hooking up multiple LED buttons:

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
int led1 = 13;
int led2 = 12;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  digitalWrite(led1,buttonState);
  digitalWrite(led2,1-buttonState);
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

The Scratch Equivalent:


Button triggered blink attack!


Arduino Code:
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
int led1 = 13;
int led2 = 12;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  //if the buttonState is 1, make the LEDs blink 4 times
  if(buttonState==1){
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
    delay(500);
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    delay(500);
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
    delay(500);
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    delay(500);
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
    delay(500);
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    delay(500);
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
    delay(500);
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    delay(500);
  }
  
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}
Scratch Equivalent:

1 comment:

  1. hi
    your Lesson 5: Arduino LED Passcode Pad
    Using a button to turn on an LED is very helpfull for me
    thanks

    ReplyDelete