Saturday, May 16, 2015

Hawaiian Inventors Club, Arduino and Beyond: Whisker Code


/*****************************************************************
* Arduino Controlled Servo Robot (SERB) - Whisker Avoidance *
* For more details visit: http://www.oomlout.com/serb *
* *
* Behaviour: Uses two wire whiskers, to bump into obstacles and *
* change direction accordingly. It will poll the *
* whiskers and when one is found to be tripped it *
* will reverse for 500 milliseconds then *
* turn randomly left or right for a random time *
* between 300 and 1500 milliseconds *
* *
* Wiring: --Arduino Controlled Servo Robot-- *
* Right Servo Signal - pin 9 *
* Left Servo Signal - pin 10 *
* --Whisker Wiring-- *
* right whisker - pin 6 *
* left whisker - pin 7 *
* whisker common - ground *
* *
* License: This work is licenced under the Creative Commons *
* Attribution-Share Alike 3.0 Unported License. To *
* view a copy of this licence, visit *
* http://creativecommons.org/licenses/by-sa/3.0/ *
* or send a letter to Creative Commons, 171 Second *
* Street, Suite 300, San Francisco, California 94105, *
* USA. *
* *
****************************************************************/

//----------------------------------------------------------------
// START OF WHISKER PREAMBLE

#define RIGHTWHISKER 6 //the pin the right whisker is attached to
#define LEFTWHISKER 7 //the pin the left whisker is attached to

//----------------------------------------------------------------
// START OF ARDUINO CONTROLLED SERVO ROBOT (SERB) PREAMBLE
#include

#define LEFTSERVOPIN 10 //The pin the left Servo is connected to
#define RIGHTSERVOPIN 9 //The pin the right Servo is connected to

#define MAXSPEED 30 //due to the way continuous rotation servos
//work maximum speed is reached at a much
//lower value than 90 (this value will change
//depending on your servos) (for Parallax servos)
//20 will give you full range 10 makes it very
//controllable but a little slow

Servo leftServo;
Servo rightServo; ;

int leftSpeed; //sets the speed of the robot (left servos)
//a percentage between -MAXSPEED and MAXSPEED
int rightSpeed; //sets the speed of the robot (both servos)
//a percentage between -MAXSPEED and MAXSPEED
int speed = 100; //used for simple control (goForward etc.)
//a percentage between 0 and MAXSPEED

// END OF THIS PROGRAMS PREAMBLE
//----------------------------------------------------------------

void setup()
{
Serial.begin (9600); //Starts the serial port
serbSetup(); //adds the servos and prepares all
//SERB related variables
whiskerSetup();
}
/*
* The main loop
* Sends the robot forward then checks it's whiskers, if one is pressed
* the robot will reverse then turn and head forward once more
*/
void loop()
{
goForward(); //sends the robot forward
checkWhiskers(); //checks to see if a whisker is pressed
}

//------------------------------------------------------------------------
//START WHISKER ROUTINES

void whiskerSetup()
{
pinMode(RIGHTWHISKER, INPUT); //Sets the right whisker's pin to be an input
pinMode(LEFTWHISKER, INPUT); //Sets the left whisker's pin to be an input
digitalWrite(RIGHTWHISKER, HIGH);//Sets the right whisker pin's internal pullup
//resistor (this means the pin is high when
//there is no signal attached and reads low
//when a negative signal is attached
digitalWrite(LEFTWHISKER, HIGH);//Sets the left whisker pin's internal pullup
//resistor
}

void checkWhiskers()
{
if(!digitalRead(RIGHTWHISKER)){reverseAndTurn();} //if the right whisker is pressed
//then reverse and turn
if(!digitalRead(LEFTWHISKER)){reverseAndTurn();} //if the left whisker is pressed
//then reverse and turn
}

void reverseAndTurn()
{
goBackward(); //goes backward
delay(500); //for half a second
turnRandom(300,1500); //turns randomly for a time between 300 and 1500 milliseconds
}
//END OF WHISKER ROUTINES
//------------------------------------------------------------------------


//------------------------------------------------------------------------
//START OF ARDUINO CONTROLLED SERVO ROBOT (SERB) ROUTINES

/*
* sets up your arduino to address your SERB using the included
* routines
*/
void serbSetup(){
setSpeed(speed);
pinMode(LEFTSERVOPIN, OUTPUT); //sets the left servo signal pin
//to output
pinMode(RIGHTSERVOPIN, OUTPUT); //sets the right servo signal pin
//to output
leftServo.attach(LEFTSERVOPIN); //attaches left servo
rightServo.attach(RIGHTSERVOPIN); //attaches right servo
goStop();
}

/*
* sets the speed of the robot between 0-(stopped) and 100-(full speed)
* NOTE: speed will not change the current speed you must change speed
* then call one of the go methods before changes occur.
*/
void setSpeed(int newSpeed){
if(newSpeed >= 100) {
newSpeed = 100;
} //if speed is greater than 100
//make it 100
if(newSpeed <= 0) {
newSpeed = 0;
} //if speed is less than 0 make
//it 0
speed = newSpeed * MAXSPEED / 100; //scales the speed to be
//between 0 and MAXSPEED
}

/*
* sets the speed of the robots rightServo between -100-(reversed) and 100-(forward)
* NOTE: calls to this routine will take effect imediatly
*/
void setSpeedRight(int newSpeed){
if(newSpeed >= 100) {
newSpeed = 100;
} //if speed is greater than 100
//make it 100
if(newSpeed <= -100) {
newSpeed = -100;
} //if speed is less than -100 make
//it -100
rightSpeed = newSpeed * MAXSPEED / 100; //scales the speed to be
//between -MAXSPEED and MAXSPEED
rightServo.write(90 - rightSpeed); //sends the new value to the servo
}

/*
* sets the speed of the robots leftServo between -100-(reversed) and 100-(forward)
* NOTE: calls to this routine will take effect immediatly
*/
void setSpeedLeft(int newSpeed){
if(newSpeed >= 100) {newSpeed = 100;} //if speed is greater than 100
//make it 100
if(newSpeed <= -100){newSpeed = -100;}//if speed is less than -100
//make it -100
leftSpeed = newSpeed * MAXSPEED / 100;//scales the speed to be
//between -MAXSPEED and MAXSPEED
leftServo.write(90 + leftSpeed); //sends the new value to the servo
}

/*
* turns the robot randomly left or right for a random time period between
* minTime (milliseconds) and maxTime (milliseconds)
*/
void turnRandom(int minTime, int maxTime){
int choice = random(2); //Random number to decide
//between left (1) and right (0)
int turnTime = random(minTime,maxTime); //Random number for the pause
//time
if(choice == 1){ goLeft();} //If random number = 1 then turn
//left
else {goRight();} //If random number = 0 then turn
//right
delay(turnTime); //delay for random time
}


/*
* sends the robot forwards
*/
void goForward(){
leftServo.write(90 + speed);
rightServo.write(90 - speed);
}

/*
* sends the robot backwards
*/
void goBackward(){
leftServo.write(90 - speed);
rightServo.write(90 + speed);
}

/*
* sends the robot right
*/
void goRight(){
leftServo.write(90 + speed);
rightServo.write(90 + speed);
}

/*
* sends the robot left
*/
void goLeft(){
leftServo.write(90 - speed);
rightServo.write(90 - speed);
}

/*
* stops the robot
*/
void goStop(){
leftServo.write(90);
rightServo.write(90);
}

//END OF ARDUINO CONTROLLED SERVO ROBOT (SERB) ROUTINES
//---------------------------------------------------------------------------

Hawaiian Inventors Club, Arduino and Beyond 5


It has been quite a learning experience showing students how to build robots from scratch. They are engaged and feel empowered. One of my goals is to show my students that with a bare minimum of specifically needed components, like an Arduino board and a couple of continuous servos, you can create a robot with things around your house. As long as you can keep track of where all the wires go you don't even need the breadboard. One could just twist all the wires together, tape four double AA batteries together and there you have it, a robot.

Everything you need to learn is online. One great place to look is on instructables.com where there are a variety of technology projects others share with the world. This is where I learned how to wire our Arduino robots and to program it to move around. This site provided the diagrams, video, and code. What I did was use this knowledge and modified it to accommodate for use in my classroom. There are many other examples that will help me add to the basic robot construct.

One such addition is building a sensor from scratch. They are whiskers placed in front of the robot, like bumpers. They trigger the robot to move in a different direction when bumping into an obstacle. During class we used a wire hanger to make the bumpers and it worked. However, this was not the ideal material. It turns out that twisted wire, five strands of hardware wire, works the best. For some reason this tends to be easier to work with while also having the consistency of keeping its shape. The bumper was attached to the front of the robot and two posts were mounted to the front as contacts. As the whisker bumps into an obstacle it makes contact with one of the posts therefore completing a circuit that sends a message to the Arduino board which in turn tells the servos which direction to turn. This could be a random or a specific direction.

I could go into more detail, but I think it would be much better for those who are interested to go right to the source. Here it is:

I did find an error in the code however. I am going to enter the correction on the next post.

Monday, May 11, 2015

Hawaiian Inventors Club, Arduino and Beyond 4

Here are projects that my students have been working on lately in the club. We began the year working with electronic circuits and then experimented with the Arduino microcontroller to create circuits to blink lights. Students create a circuit to light up a diode and write the program to control the frequency of blinks and intensity of brightness. Then they upload it to the board which controls the circuit. Students are programming in C using the Sketch IDE. Now they are building robots using CDs, Velcro, and hot glue. First they help each other build the frame; then they connect the Arduino board to the servos and power source. A program is uploaded to the board for programmed operation. Students experiment with ways to modify the program to have the robot move in different directions. They are truly engineers creating a robot from scratch, actually modifying the internals of their creation, wires, sensors and all. Future goals are to build a sensor to turn the robot, add another sensor to follow a line, add a sweeping arm, and experiment with other materials to build the same robot. Since the Arduino board is not permanently mounted on any one project we can use the board for countless other tasks.


http://hawaiianfifthgrade.weebly.com/arduino-inventors-lab.html

Hawaiian Inventors Club, Arduino and Beyond 3

Well, this is week three of exploration into the realm of electronics using the Arduino microprocessor board, the Elenco Electronic Lab, and the Elenco Snap Electronics Lab. This week students began to code using Sketch to control the behaviour of circuits made on the Arduino. Lessons learned, one has to write code correctly in order for the IDE to recognize it. Second, all components must be correctly connected to the board. Students practice following directions and learned the difference between brackets { } and braces ( ) and how to type these symbols. Also, they learned that these specific symbols have a purpose and a specific place in coding. The software is fairly easy to use and tells you what errors were made. Most students were successful and the others realized what they did wrong. Next week they will fix it. The first circuit involved controlling a flashing LED. Students also really enjoyed the Elenco kits. You can easily see the flow and construction of circuits with these kits. Students drew these circuits in their journal entries and you can see the flow in their drawings.  I was pleased to see that my students were enthusiastic about drawing and writing about what they learned. Next week I will include pictures in my blog.
Until then, Straight Ahead!

Hawaiian Technology Inventors Club, Arduino and Beyond 2

If you are interested in learning about Arduino, check out the following resources:
Introduction to Arduino
http://home.hit.no/~hansha/documents/lab/Lab%20Equipment/arduino/In...
Sparkfun SIK Guide
http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Kits/SFE03-0012-SIK....
The Official Arduino Site
http://www.arduino.cc/
There are many resources online you can use to learn about how to use Arduino.
The trick is how to introduce this technology to students.

Hawaiian Technology Inventors Club, Arduino and Beyond 1

My post from Classroom 2.0

On Tuesday, October 7th the Hawaiian Technology Inventors Club will begin. Fifth grade students will be involved in exploring hands-on activities using various electronic kits to create electronic circuits and mini projects. Most projects will utilize software and text sources available through the vast resources in the open source community. This is the beginning of a science lab experience that I hope to grow that will include many more activities in the future.
Here is what is included in the lab so far. First of all, the computing platform that is in place  is the open source, Linux based Ubuntu operating system. With this system, all the software needed for production of quality presentations, from essays, art, photo manipulation, and pamphlets is available. Also, all coding software and programming languages, from Python to Java and basic Scratch is easily accessible and free. We will use the Arduino embedded micro controller as one of the platforms to learn and explore the world of programming. One reason for this choice is that students can not only put together basic circuits, but can program the parameters, frequency, and specific tasks of the performance in each circuit project created. All this is done on the Arduino Uno board and the IDE called Sketch, based mostly in C programming language. Later, I am going to look deeper into integrating the ROS library for robotics. Arduino already has a library for this, however ROS is an international robotics standard. Anyhow, the Arduino platform can be utilized in countless ways from lighting one bulb to a multifunctional robot that speaks and blue tooth controlled, by phone, computer, tablet... you get the picture.Your imagination is the only limitation. Other kits included are the Elenco electronics lab and the Elenco Snap On electronics lab. I chose the first one because you actually put together wires to create circuits (real world). The second Snap On kit is great to see the flow and the circuit itself. The last kit I have is an electronics junk box with various speakers, wire, batteries, lights, clothespins (for switches, holding light bulbs etc.), paper clips, wire cutters, magnets, bolts, on and on; it grows as I write. This is where students just put stuff together to make it work based on what they have learned previously.
The fabulous thing about this is that students can go home and do this stuff as well at a minimal cost. Even if they purchased the kits, Elenco or Arduino, we are only talking about $20 to $60 or cheaper if you go and buy everything at Alibaba.
One last detail, other elements in the lab will include using GIMP (art, image processing), Python for Kids, Audacity (multitrack sound processing, and Openshot (video, picture editing), all open source and readily available.
We will see how it works.

Thursday, December 24, 2009

Reflection on Last Year Sept. 2008- Dec.2009 #2

Integrating technology into the classroom has changed the way we research and learn about new things. Our lab provides an up to date link to the world where we can find answers to most of our questions. We use the internet to find facts about states for our state reports. On our website, http://hawaiianfifthgrade.weebly.com/, we have links to specific sites to find this informations such as http://geology.com/state-map/, http://www.enchantedlearning.com/usa/states/, http://www.kidinfo.com/Geography/USA.html, and http://en.wikipedia.org/wiki/Wikipedia:WikiProject_U.S._states/state_templates. Students record their information on a study guide for their report. Students also use information from the internet to find out facts about molecules. They are given a molecule to research. After finding facts about their molecule, they build a model based on the facts they have discovered. We use the following site as a reference: http://www.chemicalelements.com/index.html. These are a couple of examples of research activities that we do in our classroom. There are times when my students ask a question and I either have a vague answer or I simply have no idea what the answer is. I have the student go online to find the answer. We have resources to find answers instantly and students can find the answers for themselves without having the teacher finding it for them. They share their answers with the class. My students are empowered to learn and have the world as a resource.