Okay so this is my first attempt at coding something for Arduino myself and it is an extension of the last example of getting started with Arduino.
#define SENSOR 0
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12
int val = 0; // variable to store the value coming from the sensor
int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;
int i = 0;
byte r = 0;
byte g = 0;
byte b = 0;
void setup() {
Serial.begin(9600); // open the serial port
pinMode(BUTTON, INPUT);
}
void loop() {
val = analogRead(SENSOR); // read the value from the sensor
Serial.println(val); // print the value to
// the serial port
if (Serial.available() > 0) {
// read the incoming byte:
inByte = Serial.read();
// If the marker's found, next 6 characters are the colour
if (inByte == '#') {
while (pointer < 6) { // accumulate 6 chars
buffer[pointer] = Serial.read(); // store in the buffer
pointer++; // move the pointer forward by 1
}
// now we have the 3 numbers stored as hex numbers
// we need to decode them into 3 bytes r, g and b
r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0; // reset the pointer so we can reuse the buffer
}
}
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
analogWrite(R_LED, r); // turn the leds on
analogWrite(G_LED, g); // at the colour
analogWrite(B_LED, b); // sent by the computer
}
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
delay (1000);
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(R_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(R_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(G_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(G_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(B_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(B_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
}
else {
analogWrite(R_LED, 0); // otherwise turn off
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
}
delay(100); // wait 100ms between each send
}
int hex2dec(byte c) { // converts one HEX character into a number
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
}
Now the idea is that the three lights will fade in and fade out if there is no serial data, however if there is serial data, then it assigns the brightness from that data. The problem being:
When run without serial everything works as expected. However when run with serial data one light flashes every millisecond for 500 or so milliseconds and then moves onto the next. I believe I have some problem with the if statement that is causing it to run through the fades even when there is serial data.
Any help would be greatly appreciated, thanks in advance.
#define SENSOR 0
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12
int val = 0; // variable to store the value coming from the sensor
int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;
int i = 0;
byte r = 0;
byte g = 0;
byte b = 0;
void setup() {
Serial.begin(9600); // open the serial port
pinMode(BUTTON, INPUT);
}
void loop() {
val = analogRead(SENSOR); // read the value from the sensor
Serial.println(val); // print the value to
// the serial port
if (Serial.available() > 0) {
// read the incoming byte:
inByte = Serial.read();
// If the marker's found, next 6 characters are the colour
if (inByte == '#') {
while (pointer < 6) { // accumulate 6 chars
buffer[pointer] = Serial.read(); // store in the buffer
pointer++; // move the pointer forward by 1
}
// now we have the 3 numbers stored as hex numbers
// we need to decode them into 3 bytes r, g and b
r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0; // reset the pointer so we can reuse the buffer
}
}
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
analogWrite(R_LED, r); // turn the leds on
analogWrite(G_LED, g); // at the colour
analogWrite(B_LED, b); // sent by the computer
}
while ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(R_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(R_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(G_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(G_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(B_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(B_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
}
if (Serial.available() > 0) {
// read the incoming byte:
inByte = Serial.read();
// If the marker's found, next 6 characters are the colour
if (inByte == '#') {
while (pointer < 6) { // accumulate 6 chars
buffer[pointer] = Serial.read(); // store in the buffer
pointer++; // move the pointer forward by 1
}
// now we have the 3 numbers stored as hex numbers
// we need to decode them into 3 bytes r, g and b
r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0; // reset the pointer so we can reuse the buffer
}
}
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
analogWrite(R_LED, r); // turn the leds on
analogWrite(G_LED, g); // at the colour
analogWrite(B_LED, b); // sent by the computer
delay(9899);
}
else {
analogWrite(R_LED, 0); // otherwise turn off
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
}
delay(100); // wait 100ms between each send
}
int hex2dec(byte c) { // converts one HEX character into a number
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
}
Changed to a while statement, now I just have to work out the delay because after serial data stops it takes around 20 second for the lights to cycle again.
#define SENSOR 0
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12
int val = 0; // variable to store the value coming from the sensor
int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;
int i = 0;
byte r = 0;
byte g = 0;
byte b = 0;
void setup() {
Serial.begin(9600); // open the serial port
pinMode(BUTTON, INPUT);
}
void loop() {
val = analogRead(SENSOR); // read the value from the sensor
Serial.println(val); // print the value to
// the serial port
if (Serial.available() > 0) {
// read the incoming byte:
inByte = Serial.read();
// If the marker's found, next 6 characters are the colour
if (inByte == '#') {
while (pointer < 6) { // accumulate 6 chars
buffer[pointer] = Serial.read(); // store in the buffer
pointer++; // move the pointer forward by 1
}
// now we have the 3 numbers stored as hex numbers
// we need to decode them into 3 bytes r, g and b
r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0; // reset the pointer so we can reuse the buffer
}
}
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
analogWrite(R_LED, r); // turn the leds on
analogWrite(G_LED, g); // at the colour
analogWrite(B_LED, b); // sent by the computer
}
while ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
analogWrite(R_LED, 0); // turn the leds off
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(R_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(R_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(G_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(G_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(B_LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(B_LED, i); // set the LED brightness
delay(10); // Wait 10ms
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
}
}
}
if (Serial.available() > 0) {
// read the incoming byte:
inByte = Serial.read();
// If the marker's found, next 6 characters are the colour
if (inByte == '#') {
while (pointer < 6) { // accumulate 6 chars
buffer[pointer] = Serial.read(); // store in the buffer
pointer++; // move the pointer forward by 1
}
// now we have the 3 numbers stored as hex numbers
// we need to decode them into 3 bytes r, g and b
r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0; // reset the pointer so we can reuse the buffer
}
}
btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn; // val is now old, let's store it
if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
analogWrite(R_LED, r); // turn the leds on
analogWrite(G_LED, g); // at the colour
analogWrite(B_LED, b); // sent by the computer
delay(1000);
}
else {
analogWrite(R_LED, 0); // otherwise turn off
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
}
delay(100); // wait 100ms between each send
}
int hex2dec(byte c) { // converts one HEX character into a number
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
}
Why did you buy the Arduino in the first place? What are your interests? A heads up display for a motorcycle helmet would not be a good suggestion if you hate motorcycles. What you like will have a big influence on choosing a non-trivial project that you will see through to completion.
My Idea was to build a small biped that can detect a soccer ball and kick it back to you, but I think thats beyond my skill right now. What I'm asking is what might be a good beginner/intermediate project. I bought the Arduino, because I am a web designer, however thought it would be cool to get involved in the world of physical programming. I was also considering running a chatbot on the biped so that it could have a conversation.. I have pretty much nixed that because there are no voice recognition shilds that can interperet the entire english language, the speech it not the problem there. I'm looking for a good suggestion that would help me continue to build my skills at programming and utilizing the Arduino.
On the playground, there a whole list of project ideas. The forum is full of interesting ideas. Look the exhibitions section. See if you can go one better on any of those ideas. The project guidance section is full of ideas, too.
Got a school nearby? Do they have a robotics class? Go volunteer. There are dozens of ways to find ideas.
You can try using BitVoicer (http://www.bitsophia.com/BitVoicer.aspx) for the speech recognition and, based on what it sends to your Arduino, you can use a text-to-speech (synthesizer) board to provide answers. BitVoicer's Voice Schemas can handle hundreds of recognition possibilities, so if you do a good job with the Voice Schema, it might be possible to handle simple dialogs.
Finished the whole book in one day, with the kit from Maker Shed. Any suggestions for a next project?
Welcome to the arduino world.
Sounds like your ready to take on something more challenging, perhaps an arduino controlled nuclear power plant? We will help (via forum only) if you run into snags or bugs.