-A +A

 

Updates

Who's Doing What?

Looks like everyone who wants to do a project on their own can do so without our needing a lottery for avaiable Arduinos.

Code Examples

Map Command

BIG TEXT | SMALL TEXT

Download map_command.txt file

// Example of the map() Command

// Variables
int potPin = 2;
int potVal = 0;
int mapVal = 0;

// Setup
void setup(){
  Serial.begin(9600);
}

// Das Loop...
void loop() {
 potVal = analogRead(potPin);
 // syntax: map(originalValue, fromLow, fromHigh, toLow, toHigh)
 mapVal = map(potVal, 0, 1023, 0, 180);
 Serial.print("Original value: ");
 Serial.print(potVal);
 Serial.print(", mapped value: ");
 Serial.println(mapVal);
}

Switch: Hold to Change

BIG TEXT | SMALL TEXT

Download switch_hold01.txt file

// Using a momentary switch to change behavior 
// while holding it down

// Variables
int mySwitch = 0;
int switchPin = 8;

// Setup
void setup() {
  Serial.begin(9600);
  pinMode(switchPin, INPUT);
}

// Das loop...
void loop() {
  // Look for the switch's status in every cycle of the loop.
  // Note that the switch is set to be normally HIGH
  mySwitch =  digitalRead(switchPin);
  Serial.println(mySwitch);
}

Switch: Press to Toggle

BIG TEXT | SMALL TEXT

Download switch_toggle0.txt file

// Using a momentary switch to alternate behavior 
// each time it is pressed

// Variables
int mySwitch = 0;
int switchPin = 8;
// Variables to track toggling of the switch
int whichToggle = 1; 
int toggleFlag = 1;

// Setup
void setup() {
  Serial.begin(9600);
  pinMode(switchPin, INPUT);
}

// Das loop...
void loop() {
  // Look for the switch's status in every cycle of the loop.
  // Note that the switch is set to be normally HIGH
  mySwitch =  digitalRead(switchPin);
  // Toggling a switch presents a problem. If you hold down the switch,
  // it will toggle back and forth while it's held down unless you
  // use another variable (toggleFlag) to track whether or not the switch 
  // has been released, and use that variable to keep the toggle from 
  // occuring while the switch is held down.
  if(!mySwitch) {                  // Check if the switch is pressed        
    if (toggleFlag) {              // Check if toggleFlag is true.
      whichToggle = !whichToggle;  // If it is, then toggle whichToggle
      toggleFlag = 0;              // and turn toggleFlag off.
    }
  }else{
    toggleFlag = 1;                // Reset toggleFlag when the switch
  }                                // is released.
  Serial.println(whichToggle);    
}

Switch: Toggle between two functions

BIG TEXT | SMALL TEXT

Download switch_2functions01.txt file

// Using a momentary switch to run different functions 
// each time it is pressed.

// Variables
int mySwitch = 0;
int switchPin = 8;
// Variables to track toggling of the switch
int whichToggle = 1; 
int toggleFlag = 1;
// Timer variables
long timer = 0;
int offset = 1000;

// Setup
void setup() {
 Serial.begin(9600);
 pinMode(switchPin, INPUT);
 timer = millis();
}

// Das loop...
void loop() {
 // Detect Switch
 mySwitch = digitalRead(switchPin);
 if(!mySwitch) { 
   if (toggleFlag) { 
     whichToggle = !whichToggle; 
     toggleFlag = 0; 
   }
 }else{
   toggleFlag = 1;
 } 
 // Timer
 if (timer + offset < millis()) {
   if(whichToggle) {
     doThingA();
   }else{
     doThingB();
   } 
 }
}

// Do Thing A
void doThingA() {
 Serial.println("Doing thing A");
} 

// Do Thing B
void doThingB() {
 Serial.println("Doing thing B");
} 
finB+