Hi! The activity involves me having to create an up/down counter using an Arduino and controlled by processing. My idea is that there are buttons in the processing program that when hovered over, sends a signal to the arduino program signifying a process. So hovering over the first square, makes it count down, the second square, stop, and the last square resets it. If it is not over any square, it sends a '0', making it continue counting up.
This is the working arduino code. I have pushbuttons that does the processes mentioned.
`
unsigned char digit_1 = 7;
unsigned char digit_2 = 8;
unsigned char digit_3 = 10;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int state = 0;
int halt = 0;
char val; //data received from serial port
void setup()
{
Serial.begin(9600);
for (int x = 11; x < 19; x++)
{
pinMode (x, OUTPUT);
}
pinMode (digit_1, OUTPUT);
pinMode (digit_2, OUTPUT);
pinMode (digit_3, OUTPUT);
// attachInterrupt(0, s1_press, RISING);
// attachInterrupt(1, s2_press, RISING);
}
void loop()
{
while (Serial.available())
{ val = Serial.read();
}
if (val == 1)
{
state = 1;
}
else //if (val == 0)
{
state = 0;
halt = 0;
}
if (val == 2)
{
halt = 1;
}
else
{
state = 0;
halt = 0;
}
if (halt == 0) {
if (state == 0) {
num3++;
if (num3 == 10) {
num3 = 0;
num2++;
}
if (num2 == 10) {
num2 = 0;
num1++;
}
if (num1 == 10) {
num1 = 0;
num2 = 0;
num3 = 0;
}
}
if (state == 1) {
num3--;
if (num3 == -1) {
num3 = 9;
num2--;
}
if (num2 == -1) {
num2 = 9;
num1--;
}
if (num1 == -1) {
num3 = 9;
num2 = 9;
num1 = 9;
}
}
}
for (int x = 0; x < 80; x++) {
digitalWrite (digit_3, HIGH);
digitalWrite (digit_2, LOW);
digitalWrite (digit_1, LOW);
display_ (num1);
delay(1);
digitalWrite (digit_3, LOW);
digitalWrite (digit_2, HIGH);
digitalWrite (digit_1, LOW);
display_ (num2);
delay(1);
digitalWrite (digit_3, LOW);
digitalWrite (digit_2, LOW);
digitalWrite (digit_1, HIGH);
display_ (num3);
delay(1);
}
}
void display_ (unsigned char num)
{
switch (num)
{
case 0:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, HIGH);
digitalWrite (17, LOW);
digitalWrite (18, LOW);
break;
}
case 1:
{
digitalWrite (11, LOW);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, LOW);
digitalWrite (15, LOW);
digitalWrite (16, LOW);
digitalWrite (17, LOW);
digitalWrite (18, LOW);
break;
}
case 2:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, LOW);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, LOW);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 3:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, LOW);
digitalWrite (16, LOW);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 4:
{
digitalWrite (11, LOW);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, LOW);
digitalWrite (15, LOW);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 5:
{
digitalWrite (11, HIGH);
digitalWrite (12, LOW);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, LOW);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 6:
{
digitalWrite (11, HIGH);
digitalWrite (12, LOW);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 7:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, LOW);
digitalWrite (15, LOW);
digitalWrite (16, LOW);
digitalWrite (17, LOW);
digitalWrite (18, LOW);
break;
}
case 8:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 9:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, LOW);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
}
}
void s1_press() {
if (state == 0)
state = 1;
else if (state == 1)
state = 0;
delay (100);
}
void s2_press() {
if (halt == 0)
halt = 1;
else
halt = 0;
delay(100);
}
Here is my intended implementation of the processing code based on the serial write example;
`
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
color c1 = #75C58E;
color c2 = #75C5FF;
color c3 = #D07633;
void setup()
{
size(420, 200);
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}
//0 - count up and start
//1 - count down
//2 - stop
//3 - reset
void draw() {
background(255);
if (mouseOverRect() == true) { // If mouse is over square,
fill(c1); // change color and
myPort.write('1'); // send a '1' to indicate mouse is over square
} else //will be equivalent to the count up/count down trigger
{
fill(0);
myPort.write('0');
}
rect(50, 50, 100, 100); // Draw a square
if (mouseOverRect2() == true) { // If mouse is over square,
fill(c2); // change color and
myPort.write('2'); // send a 2 to indicate mouse is over square
} else //stop trigger
{
fill(0);
myPort.write('0');
}
rect(160, 50, 100, 100);
if (mouseOverRect3() == true) { // If mouse is over square,
fill(c3); // change color and
myPort.write('3'); // send a '3' to indicate mouse is over square
} else //reset
{
fill(0);
myPort.write('0');
}
rect(270, 50, 100, 100);
}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect2() { // Test if mouse is over square
return ((mouseX >= 160) && (mouseX <= 260) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect3() { // Test if mouse is over square
return ((mouseX >= 270) && (mouseX <= 370) && (mouseY >= 50) && (mouseY <= 150));
}
`
Any tips on how this could be implemented? My implementation doesn't seem to affect the arduino program in any way.