I have an arduino uno connected to a 8 channel relay board. I want to use it with Vixen 3. When I upload the code all the relays turn on. So, when i send a signal in Vixen the relay turns off. I need a way to invert this so when I send the signal from Vixen it turns on the relay. The coding side isn't really my strong point, so please go easy on me.
With love xx
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i=0; i<=8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}
Try this code:
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
digitalWrite(C1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C3, LOW);
digitalWrite(C4, LOW);
digitalWrite(C5, LOW);
digitalWrite(C6, LOW);
digitalWrite(C7, LOW);
digitalWrite(C8, LOW);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i = 0; i <= 8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}
Related
I have been working on getting some motors to work through H-bridges and managed to with this code.
// initialise motors
int enA = 3; // Motor 1
int in1 = 4;
int in2 = 2;
int enB = 5; // Motor 2
int in3 = 8;
int in4 = 7;
int enC = 11; // Motor 3
int in5 = 12;
int in6 = 13;
int enD = 6; // Motor 4
int in7 = 9;
int in8 = 10;
void setup()
{
pinMode(enA, OUTPUT); // set the outputs for motors
pinMode(enB, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(enD, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);
}
void motorLoop(){
// setting the direction to turn and speed
digitalWrite(in1, HIGH); // Motor 1
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); // Motor 2
digitalWrite(in4, LOW);
digitalWrite(in5, HIGH); // Motor 3
digitalWrite(in6, LOW);
digitalWrite(in7, HIGH); // Motor 4
digitalWrite(in8, LOW);
// Set the speed for the Motors
analogWrite(enA, 1);
analogWrite(enB, 20);
analogWrite(enC, 100);
analogWrite(enD, 200);
};
void loop()
{
motorLoop();
delay(500);
}
However I am trying to turn the data into an array and have hit some issues.
I have never tried creating arrays with Digital inputs but have with Analog ones.
Here is a link to my project on (TinkerCAD) https://www.tinkercad.com/things/fFQKRTjhDrb-smashing-allis-kieran/editel?tenant=circuits?sharecode=6rKnUZsFtcOAetd_TufIuN8TfUgi8EupA1TMjlxiacM=
As you can see by this code I have tried to setup the enable inputs which show no errors but I am struggling setting up the OUTPUT and Speed that the motors rotate at.
// initialise motors
// Motor 1
int in1 = 4;
int in2 = 2;
// Motor 2
int in3 = 8;
int in4 = 7;
// Motor 3
int in5 = 12;
int in6 = 13;
// Motor 4
int in7 = 9;
int in8 = 10;
// Array of PWM's
int i = 0;
byte pwms[i] = {3,5,6,11};
byte numberPwms = 4;
void setup()
{
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);
for(byte i = 0; i <= numberPwms; i++){
pinMode(pwms[i], OUTPUT);
};
}
void motorLoop(){
// setting the direction to turn and speed
digitalWrite(in1, HIGH); // Motor 1
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); // Motor 2
digitalWrite(in4, LOW);
digitalWrite(in5, HIGH); // Motor 3
digitalWrite(in6, LOW);
digitalWrite(in7, HIGH); // Motor 4
digitalWrite(in8, LOW);
// Set the speed for the Motors
for(byte i = 0; i < numberPwms; i++){
analogWrite(pwms[i], 200);
};
};
void loop()
{
motorLoop();
delay(500);
}
Any help with this would be greatly appreciated :D
Your for loop counts 5 times. Try changing it to this:
for (byte i = 0; i < numberPwms; i++) {
pinMode(pwms[i], OUTPUT);
};
Also your array pwms[] is initialized with 0 elements, but should work anyway. But you can change it.
uint8_t pwms[] = {3, 5, 6, 11};
I have a set of LEDs 12 in total: 6 are blue, 6 are red. I had for loops that helped with the clutter for turning the LEDs on and off at a set interval. But now I want to make it so it has while loops controlling how long until the LED's speed changes. I have made 2 so far and both of them work but the second I put the for loop inside it doesn't do anything.
int redLEDPins[] = {2,3,4,5,6,7};
int blueLEDPins[] = {8,9,10,11,12,13};
int LED_Amount = 6;
int led_delay = 1000;
unsigned long time_since_last_reset = 0;
int wail = 5000;
int yelp = 3000;
int phaser = 3000;
int hilo = 3000;
void setup() {
for (int i; i < LED_Amount; i++) {
pinMode(redLEDPins[i], OUTPUT);
pinMode(blueLEDPins[i], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
//wail while loop
time_since_last_reset = millis();
while((millis() - time_since_last_reset) < wail) {
led_delay = 250;
Serial.print("delay: ");
Serial.println(led_delay);
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}
//yelp while loop
time_since_last_reset = millis();
while((millis() - time_since_last_reset) < wail){
led_delay = 50;
Serial.print("delay: ");
Serial.println(led_delay);
for (int i; i < LED_Amount; i++) {
digitalWrite(redLEDPins[i], HIGH);
digitalWrite(blueLEDPins[i], LOW);
Serial.println("Red on ");
Serial.println("Blue off");
}
delay(led_delay);
for (int i; i < LED_Amount; i++){
digitalWrite(redLEDPins[i], LOW);
digitalWrite(blueLEDPins[i], HIGH);
Serial.println("Red off ");
Serial.println("Blue on");
}
delay(led_delay);
}
}
for (**int i**; i < LED_Amount; i++){
digitalWrite(redLEDPins[i],LOW);
digitalWrite(blueLEDPins[i],HIGH);
Serial.println("Red off ");
Serial.println("Blue on");
}
Might that be because you didn't initialize i?
I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.
]1
const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;
char keys[kbdRows][kbdCols] = {
{ '1','2','3','4' },
{ '5','6','7','8' },
{ '9','0','A','B' },
{ 'C','D','E','F' }
};
byte KeyIsDown() {
int row;
int col;
int rowBits;
int colBits;
rowBits = 0X10;
for (row = 0; row < kbdRows; row++) {
digitalWrite(ClockPin, LOW);
digitalWrite(LatchOut, LOW);
shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
digitalWrite(LatchOut, HIGH);
delay(5);
digitalWrite(ClockPin, HIGH);
digitalWrite(LatchIn, LOW);
delay(5);
digitalWrite(LatchIn, HIGH);
colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
for (col = 0; col < kbdCols; col++) {
if (colBits==0) {
// not sure what condition to put here
byte keypressed = keys[kbdRows][kbdCols]; here
// I know this is the right stuff to return here
}
return colBits;
colBits = colBits >> 1;
}
rowBits = rowBits << 1;
}
}
void setup() {
pinMode(ClockPin, OUTPUT);
pinMode(DataOut, OUTPUT);
pinMode(DataIn, INPUT_PULLUP);
pinMode(LatchOut, OUTPUT);
pinMode(LatchIn, OUTPUT);
digitalWrite(LatchOut, HIGH);
digitalWrite(LatchIn, HIGH);
Serial.begin(9600);
digitalWrite(led, HIGH);
}
void loop() {
byte retColBit = KeyIsDown();
Serial.print("ColBit: ");
Serial.println(retColBit,BIN);
delay(500);
PinState = digitalRead(DataOut);
Serial.print("DataOut: ");
Serial.println(PinState,BIN);
delay(500);
}
I am sending 2 bytes from Processing to Arduino. These bytes are then stored in two values on the Arduino side, and then sent to a function that will then display them on a 7 segment 4 digit display.
The numbers are displayed fine! Yes, until about 5 seconds. I recorded it in slow motion.
9561 runs for about 5 seconds,
then a flash of 6100 very shortly,
and then 6195 very shortly
then it returns to 9561 for about 5 seconds again.
This can be changed slightly by altering the time each number is on the display, and I try to keep it below 4.
Anyways then changing the speeds on both programs also alters it, but I can't seem to get it right. Do I have to sync the programs through altering the speeds or is there a way to sync the numbers? I read into serialEvent() but not sure how to incorporate it into my project.
Here's my code below for the PROCESSING
void setup()
{
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
seg[0] = 9;
seg[1] = 5;
seg[2] = 6;
seg[3] = 1;
int x = (seg[pos+1] & 0x000f)<<4|(seg[pos]) & 0x000f; //3210
int x2 = (seg[pos+3] & 0x000f)<<4|(seg[pos+2] & 0x000f);
myPort.write(x);
myPort.write(x2);
//0123
int disp1 = x;
int disp2 = (x & 0x00f0)>>4;
int disp3 = (x2 & 0x000f);
int disp4 = (x2 & 0x00f0)>>4;
//0123
print(disp1, '\n', disp2, '\n', disp3, '\n', disp4, '\n', '\n');
}
And ARDUINO
void setup() {
pinMode(GND1, OUTPUT);
pinMode(GND2, OUTPUT);
pinMode(GND3, OUTPUT);
pinMode(GND4, OUTPUT);
pinMode(aPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(cPin, OUTPUT);
pinMode(dPin, OUTPUT);
pinMode(ePin, OUTPUT);
pinMode(fPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(light, OUTPUT);
Serial.begin(9600);
}
void digitdisplay(byte x,byte x2)
{
//unsigned char tn = int (num/1000);
//if (tn>10)
//{
// tn = tn - 10;
//}
//
//unsigned char hn = int (num/100)%10;
//unsigned char tenn = int (num/10)%10;
//unsigned char on = int (num%10);
int disp[4];
disp[0] = ( x & 0x000f);
disp[1] = ( x & 0x00f0)>>4;
disp[2] = ( x2 & 0x000f);
disp[3] = ( x2 & 0x00f0)>>4;
numberselect(disp[0]);
digitselect(1);
delay(t);
numberselect(disp[1]);
digitselect(2);
delay(t);
numberselect(disp[2]);
digitselect(3);
delay(t);
numberselect(disp[3]);
digitselect(4);
delay(t);
}
void loop()
{
int x,x2;
if (Serial.available()>0)
{ // If data is available to read,
x=Serial.read();
x2=Serial.read();
}
//shiftlight(maxrange);
digitdisplay(x,x2);
}
Actually in my code I'm transmitting accelerometer values as well as 4 flex sensor values. I have converted accelerometer value to 4 state as F(forward), B(backward),R(right),L(left). I am able to receive this 4 states and also I'm able to write code for these states. With these states I'm also sending flex sensor values to control servo motor remotely. But I'm not able to read those flex values as it is a varying integer. Please help me to read the flex values. I have tried Serial.read(), Serial.parseInt().
My transmitter code is:
int xpin = A0;
int x;
int ypin = A1;
int y;
int vcc=13;
const int flexpin1 = 2;
const int flexpin2 = 3;
const int flexpin3 = 4;
const int flexpin4 = 5;
int flex1[20];
int flex2[20];
int flex3[20];
int flex4[20];
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;
char state1 = 'S';
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(flexpin1, INPUT);
pinMode(flexpin2, INPUT);
pinMode(flexpin3, INPUT);
pinMode(flexpin4, INPUT);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(vcc, OUTPUT);
digitalWrite(vcc, HIGH);
}
void loop() {
x = analogRead(xpin);
Serial.print("xpin=");
Serial.print(x);
y = analogRead(ypin);
Serial.print("\t ypin=");
Serial.print(y);
if ( y > 415)
{
state1 = 'F';
}
else if (y < 315)
{
state1 = 'B';
}
else if (x > 410)
{
state1 = 'R';
}
else if (x < 310)
{
state1 = 'L';
}
else {
state1 = 'S';
}
// flex readings stabelized//
for(int x=0; x<25; x++)
{
flex1[x]=analogRead(flexpin1);
flex2[x]=analogRead(flexpin2);
flex3[x]=analogRead(flexpin3);
flex4[x]=analogRead(flexpin4);
flexsum1=flexsum1+flex1[x];
flexsum2=flexsum2+flex2[x];
flexsum3=flexsum3+flex3[x];
flexsum4=flexsum4+flex4[x];
delayMicroseconds(20);
}
flexsum1=flexsum1/25;
flexsum2=flexsum2/25;
flexsum3=flexsum3/25;
flexsum4=flexsum4/25;
Serial.print("\t\t\tflexsum1= ");
Serial.print(flexsum1);
Serial.print("\tflexsum2= ");
Serial.print(flexsum2);
Serial.print("\tflexsum3= ");
Serial.print(flexsum3);
Serial.print("\tflexsum4= ");
Serial.print(flexsum4);
Serial.print("\t state1=");
Serial.println(state1);
delay(200);
}
My receiving code:
#include<Servo.h>
int IN1 = 13;
int IN2 = 12;
int IN3 = 7;
int IN4 = 6;
char state1 = 'S';
Servo servo1, servo2, servo3, servo4;
int flex1, flex2, flex3, flex4;
int angle1, angle2, angle3, angle4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servo1.attach(9);
servo2.attach(8);
servo3.attach(10);
servo4.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()) {
delay(10);
state1 = Serial.read();
Serial.print(state1);
switch (state1)
{
case 'F' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is moving forward ");
delay(100);
break;
case 'B' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is moving backward ");
delay(100);
break;
case 'L' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is turning left ");
delay(100);
break;
case 'R' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is turning right");
delay(100);
break;
case 'S' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , LOW);
delay(100);
break;
}
if (flex1 >= 845)
{
flex1 = Serial.read();
angle1 = map(flex1, 848, 1000, 0, 180);
angle1 = constrain(angle1, 0, 90);
servo1.write(angle1);
Serial.print("\tangle1=");
Serial.print(angle1);
delay(200);
}
if (flex2 >= 820)
{
flex2 = Serial.read();
angle2 = map(flex2, 825, 1000, 0, 180);
angle2 = constrain(angle2, 0, 90);
servo2.write(angle2);
Serial.print("\tangle2=");
Serial.print(angle2);
delay(200);
}
if (flex3 >= 770)
{
flex3 = Serial.read();
angle3 = map(flex3, 770, 930, 0, 180);
angle3 = constrain(angle3, 90, 180);
servo3.write(angle3);
Serial.print("\tangle3=");
Serial.print(angle3);
delay(200);
}
if (flex4 >= 870)
{
flex4 = Serial.read();
angle4 = map(flex4, 875, 1020, 0, 180);
angle4 = constrain(angle4, 90, 180);
servo4.write(angle4);
Serial.print("\tangle4=");
Serial.print(angle4);
delay(200);
}
}
}