I'm trying to build a system using Arduino Uno, 8x8 LED matrix and 3 push buttons. The goal of the system is to display 3 different characters upon pushing the 3 buttons correspondingly. For instance, I've chosen the letters A,B,C. When the button corresponding to A is pressed, the letter A must be displayed and similar for B and C too. I'm kinda stuck in this code, where it seems logically correct for me but I have no idea why it isn't working. Thanks in advance.
#include "LedControlMS.h"
#define NBR_MTX 1
LedControl lc=LedControl(4,3,2, NBR_MTX);//
const int buttonPinA = 8;
const int buttonPinB = 9;
const int buttonPinC = 10;
char ip2;
void setup()
{
Serial.begin(9600);
for (int i=0; i< NBR_MTX; i++)
{
lc.shutdown(i,false);
lc.setIntensity(i,8);
lc.clearDisplay(i);
delay(100);
}
}
void Fun1()
{
lc.writeString(0,"A");
delay(500);
lc.clearAll();
}
void Fun2{
lc.writeString(0,"B");
delay(500);
lc.clearAll();
}
void Fun3()
{
lc.writeString(0,"C");
delay(500);
lc.clearAll();
}
void loop(){
if( digitalRead(buttonPinA) == HIGH){
ip2 = 1;}
else if(digitalRead(buttonPinB) == HIGH){
ip2 = 2;}
else if(digitalRead(buttonPinC) == HIGH){
ip2 = 3;
}
if(ip2 == '1'){
for(int i=1;i<=6;i++){
Fun1();
}
}
else if(ip2 == '2')
{
for(int i=1;i<=6;i++){
Fun2();}
}
else if(ip2 == '3'){
for(int i=1;i<=6;i++){
Fun3();}
}}
You're setting your char ip2 variable as an integer and then checking it as a character. You will see in this ASCII Table that '1' is equal to 31 as in integer, '2' is equal to 32 and so on.
Replacing the first few lines of you main loop with the code below should fix your problem. If not, I would check the documentation of the led library you're using and make sure you're implementing it correctly.
if( digitalRead(buttonPinA) == HIGH)
{
ip2 = '1';
}
else if(digitalRead(buttonPinB) == HIGH)
{
ip2 = '2';
}
else if(digitalRead(buttonPinC) == HIGH)
{
ip2 = '3';
}
Related
I am using arduino Mega 2560 to control some vibration motors with touchdesigner through Serial Communication. I maped the pixels to control each motors, It works for a few seconds and gets stuck very soon. Is there anything wrong with my code?
Here is my arduino sketch:
#define MOTOR_COUNT 12
int motors[MOTOR_COUNT];
void setup()
{
Serial.begin(115200);
Serial.println("Ready to receive frames.");
for (int i = 0; i <= 12; i++) {
pinMode(i, OUTPUT);
}
void loop()
{
if (Serial.available())
{
char c = Serial.peek();
if (!(c >= '0' && c <= '9'))
{
Serial.read(); // Discard non-digit character
}
else if (Serial.read() == '\n')
{
for (uint16_t i = 0; i < MOTOR_COUNT; i++)
{
motors[i] = Serial.parseInt();
Serial.print("motor ");
Serial.print(i);
Serial.print(":");
Serial.println(motors[i]);
if (i <= 12) {
analogWrite(i + 2, motors[i]);
}
}
}
}
}
I am writing a piece of code in the Arduino IDE. I have an IF statement which has a long comparison of arrays to see if they are all equal to 0. If they are then carry out the code within the statement.
I'm not sure if this is possible but thought I would ask if the code can be simplified. For all the other code I have been able to use for loops, but I cannot see this working with this statement.
The statement will have about 20 arrays to compare in the end.
if (Tbit[0] == 0 && Tbit[1] == 0 && Tbit[2] == 0 && Tbit[3] == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
EDIT UPDATE
I have tried PiNaKa30 suggestion below which I thought would work but still the outputs for the HIGH statement are not being carried out. Below is the full void loop code with my code above commented out and PiNaKa30 codes added.
The bit being read at the beginning only goes HIGH '1' for 1 second when a command input to the arduino is received. Normal state is 0
void loop() {
cmri.process();
for (int i=0; i<2; i++){
Tbit[i] = (cmri.get_bit(i));
}
for (int i=0; i<2; i++){
if (Tbit[i] == 1){
digitalWrite(Tout[i], !Tbit[i]);
}
}
// if (Tbit[0] == 0 && Tbit[1] == 0){
// digitalWrite(Tout[0], HIGH);
// digitalWrite(Tout[1], HIGH);
// }
for(int i=0; i < 2; i++){
val = val || Tbit[i]; // Even a single 1 in Tbit will make val=1
}
if(val == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
}
You can maintain a variable which will be the OR of all values of the array. This variable must be updated as soon as any value changes in the array, and be used in your if statement.
int val = 0; // initialize
updateVal(){
for(int i=0;i<Tbit.length;i++)
val = val || Tbit[i]; // Even a single 1 in Tbit will make val=1
}
if(val == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
I am assuming here that Tbit can only have values 0 and 1. Otherwise change the function slightly to suit your needs.
Got it to work with the help of PiNaKa30 suggestion and a little tweeting.
void loop() {
val = 0;
cmri.process();
for (int i=0; i<2; i++){
Tbit[i] = (cmri.get_bit(i));
}
for (int i=0; i<2; i++){
if (Tbit[i] == 1){
digitalWrite(Tout[i], !Tbit[i]);
}
}
for(int i=0; i < 2; i++){
val = Tbit[i]; // Even a single 1 in Tbit will make val=1
}
if(val == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
}
Sometimes when you looking for an exception its easier to start from the other side:
I would have divided it into two parts
first create a variable to tell us result of a check
bool correct=1;
next check each if it contains any other then correct value
for(uint8_t i=0; i<**Number of array members**;i++) {
Now check if any of them are wrong, and if yes assume incorrect
if(Tbit[i]!=0) correct=0;
and now only process your code if none was incorrect
if(correct) ...
Which would at the end look like:
void loop() { // or any while statement you would have it in
cmri.process();
for (int i=0; i<2; i++){
Tbit[i] = (cmri.get_bit(i));
}
bool correct=1;
for(uint8_t i=0; i<**Number of array members**;i++) {
if(Tbit[i]!=0) correct=0;
}
if(correct) {
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
}
I am trying to make a micro-mouse maze solving robot, and I'm running into some issues with my class called Mouse. I get the error: expected ';' before 'right_motor' and 'left_motor'. I have no idea what is wrong with the code. I also am not sure exactly where I should declare my objects left and right motors. Here is all my code, thank you for your help.
Mouse.h:
#ifndef _MOUSE_H_
#define _MOUSE_H_
class Mouse
{
private:
int speed;
int motor_num;
int direction;
public:
Mouse(int motor_number);
~Mouse();
void run(int speed, int direction);
};
#endif
Mouse.cpp:
#include "mouse.h"
Mouse::Mouse(int motor_number)
{
motor_num = motor_number;
speed = 0;
direction = 0;
return;
}
// Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
// Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
// Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
// Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)
void Mouse::run(int speed, int direction)
{
int M1 = 11;
int M2 = 3;
int M3 = 5;
int M4 = 6;
if(motor_num == 1)
{
if(direction == 1)
{
analogWrite(M1, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M1, LOW);
}
}
if(motor_num == 2)
{
if(direction == 1)
{
analogWrite(M2, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M2, LOW);
}
}
if(motor_num == 3)
{
if(direction == 1)
{
analogWrite(M3, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M3, LOW);
}
}
if(motor_num == 4)
{
if(direction == 1)
{
analogWrite(M4, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M4, LOW);
}
}
return;
}
Code in the Arduino sketch:
#include "mouse.h"
void setup()
{
//begin communication with serial port:
Serial.begin(9600);
//pinmodes setup:
//declare variables:
Mouse right_motor(1);
Mouse left_motor(2);
}
void loop()
{
//assign values to their respective variables:
//BEGIN PROGRAM
}
Change your class in MyMouse (...and update all reference of it), I think Mouse is an internal Arduino library
then, pay attention on Uppercase chars sometimes Mouse is uppercase and sometimes is lowercase
Edit:
this is Mouse Arduino library
http://www.arduino.cc/en/Reference/MouseKeyboard
I need to write a program that will check if an input pin of PIC has a voltage. If a voltage exists then it will give voltage to a selected output pin like PORTB.RB1=1;. Else it will give voltage to other selected output pin like PORTC.RC1=1;.
Is it possible? I have tried to do this, but it does not work .
void main() {
TRISB=0;
TRISA=1;
TRISC=0;
while(1){
delay_ms(500);
// PORTB=0;
if(PORTA==1){
PORTB.RB1 =1;
}
else{
PORTC.RC1 =1;
}
}
}
You have not turned off the other port output, and you have not isolated the input pin of PORTA. If it's bit 0 the mask is 1, if it's bit 1 the mask is 2, etc.
void main() {
TRISB=0;
TRISA=1;
TRISC=0;
while(1){
delay_ms(500);
if(PORTA & 1){
PORTB.RB1 =1;
PORTC.RC1 =0;
}
else{
PORTB.RB1 =0;
PORTC.RC1 =1;
}
}
}
The PORTA and the PORTE are analog ports. If you want to use them as Digital Input, you must Prevent PIC to use them as analog inputs.
You must add this instruction:
ADCON1=0x06;
before you set the PORTA or PORTE as input.
This code works successfully:
void main()
{
ADCON1=0x06;
TrisA=1;
TrisE=1;
TrisC=0;
PortC=0;
while (1)
{
if (PortA.B0==1)
PortC.B0=1;
else
PortC.B0=0;
if (PortA.B1==1)
PortC.B1=1;
else
PortC.B1=0;
if (PortA.B2==1)
PortC.B2=1;
else
PortC.B2=0;
if (PortA.B3==1)
PortC.B3=1;
else
PortC.B3=0;
if (PortA.B5==1)
PortC.B4=1;
else
PortC.B4=0;
if (PortE.B0==1)
PortC.B5=1;
else
PortC.B5=0;
if (PortE.B1==1)
PortC.B6=1;
else
PortC.B6=0;
if (PortE.B2==1)
PortC.B7=1;
else
PortC.B7=0;
}
}
hardware connection : wire input port's bit with 5v supply and switch.
after this connect it with pull down resistors.
void main(){
TRISB = 1; //set portB as input
TRISC = 0; //set portC as output
while(1){
if(PORTB.B0 == 0){ //if RB0 == 0 ?
PORTC.F0 = 1; //set RC0 = 1 ,(high)
}else PORTC.F0 = 0; //set RC0 = 0 ,(low)
if(PORTB.B1 == 0){ //if RB1 == 0 ?
PORTC.F1 = 1; //set RC1 = 1 ,(high)
}else PORTC.F1 = 0; //set RC1 = 0 ,(low)
//set if else block for numbers of bit as you want.....
}
}
note that pic port A is anlalog input default and if you want to use this port as digital port change the ADCON registers and follow datasheet.
This is my first post, I know this theme could be very simple or obvious but I can't figure out how to solve it.
I have a capacitance meter from jyetech wiuch claims to have a 8-N-1 serial output, here is the link to the manual. I just want to read the output with my Arduino Uno, can anyone help me? Here is the code I have done, I'm getting some real data but also some strange characters.
#include <stdio.h>
void setup() {
Serial.begin(38400);
Serial.println("OK");
}
char command[1024];
char commandBuffer[128];
int commandBufferSize = 0;
void readCommandBuffer(int bytesToRead) {
int i = 0;
char c = 0;
while (i < 128 && (i < bytesToRead || bytesToRead <= 0)) {
while (!Serial.available())
;
c = Serial.read();
if (c == '\r' || c == '\n') {
break;
}
commandBuffer[i] = c;
i++;
}
commandBufferSize = i;
}
void readCommand() {
command[0] = '\0';
readCommandBuffer(0);
if (strncmp(commandBuffer, "RCV", 3) == 0) {
commandBuffer[commandBufferSize] = '\0';
int expectedSize = atoi(commandBuffer + 4);
if (expectedSize <= 0 || expectedSize > 1024) {
return;
}
Serial.println("RDY");
int bytesRead = 0;
while (bytesRead < expectedSize) {
readCommandBuffer(expectedSize - bytesRead);
memcpy(command + bytesRead, commandBuffer, commandBufferSize);
bytesRead += commandBufferSize;
Serial.print("ACK ");
Serial.println(commandBufferSize);
}
command[bytesRead] = '\0';
} else {
memcpy(command, commandBuffer, commandBufferSize);
command[commandBufferSize] = '\0';
}
}
void loop() {
if (Serial.available()) {
readCommand();
// "command" now contains the full command
Serial.println(command);
}}
You have to use two serial ports, one to talk to the PC (the usual Serial object), and the other to talk to the instrument.
If you have an Arduino UNO, which has only one hardware serial port, you have to use the software serial library. Mega boards have instead 4 hardware serial ports, which are available through the bulit-in objects Serial1, Serial2, etc.
BTW, just noticed a very similar question was asked awhile ago:
Serial Data communication Arduino