[Arduino][C++]Would the following code work for arduino? - arduino

#include <opencv2/opencv.hpp> // Library for working with image and video processing
#include <OpenGL/gl.h> // Library for working with 3D graphics
#include <GLUT/glut.h> // Library for working with 3D graphics
#include <TinyGPS++.h> // Library for working with GPS data
// Declare and initialize variables for camera and video processing
cv::VideoCapture cap(0);
cv::Mat frame;
// Declare and initialize variables for 3D graphics
GLuint texture;
// Declare and initialize variables for GPS data
TinyGPSPlus gps;
HardwareSerial serial(1);
void setup() {
// Initialize the camera and video capture
cap.open(0);
cap >> frame;
// Create a texture for the video frame
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, frame.data);
// Initialize the 3D graphics environment
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(frame.cols, frame.rows);
glutCreateWindow("3D Marker");
glutDisplayFunc(display);
// Initialize the serial communication with the GPS module
serial.begin(9600);
}
void loop() {
// Read a new frame from the camera
cap >> frame;
// Update the texture with the new frame
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.cols, frame.rows, GL_BGR, GL_UNSIGNED_BYTE, frame.data);
// Read data from the GPS module
while (serial.available()) {
gps.encode(serial.read());
}
// Get the GPS coordinates and altitude of the marker
double markerLatitude = gps.location.lat();
double markerLongitude = gps.location.lng();
double markerAltitude = gps.altitude.meters();
// Draw the video frame as a textured plane
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f(-1, -1, 0);
glTexCoord2f(1, 1); glVertex3f(1, -1, 0);
glTexCoord2f(1, 0); glVertex3f(1, 1, 0);
glTexCoord2f(0, 0); glVertex3f(-1, 1, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
// Draw the marker in 3D space
glPushMatrix();
glTranslatef(markerLatitude, markerLongitude, markerAltitude);
glutSolidSphere(0.1, 16, 16);
glPopMatrix();
// Swap the back and front buffers
glutSwapBuffers();
}
I wrote some code. What does this code do?:
Takes in gps coordinates from a gps reciever
Draws a sphere on a screen at given gps coords.
I don't have the equipment yet, so I am unable to check the code.
As I am only a beginner in this, I would like to ask if this would work in real circumstances...
Didn't yet try, as I dont know what to try.

Related

LCD doesn't work in a simulated enviroment

I'm using Tinkercad, and since it's my first time programming an LCD I just copied the procedure to connect the pins and make it work.
The thing is that it just lights up without displaying anything, I tried both wiring and unwiring the R/W pin but that doesn't work either, nothing will be displayed.
What did I miss? The other functions of the code works normally.
Image of the circuit:
This is the code:
#include <LiquidCrystal.h>
const int pin = 0; // analog pin
float celsius = 0, farhenheit =0; // temperature variables
float millivolts; //Millivolts from the sensor
int sensor;
const int G_LED = 13;
const int Y_LED = 12;
LiquidCrystal lcd(10, 9, 5, 4, 3, 2); // Building the LCD
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("C="); // "C=", "F=" and "mV" should be printed
lcd.setCursor(0, 1); // on the LCD in a column
lcd.print("F=");
lcd.setCursor(0, 2);
lcd.print("mV=");
pinMode(G_LED, OUTPUT);
pinMode(Y_LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensor = analogRead(pin); // Reading the value from the LM35 sensor using the A0 ingress
millivolts = (sensor / 1023.0) * 5000; // Converting the value in a number that indicates the millivolts
celsius = ((sensor * 0.00488) - 0.5) / 0.01; // Celsius value (10 mV for each degree, 0°=500mV)
farhenheit = celsius * 1.8 + 32; // Fahrenheit value
lcd.setCursor(4, 2); // Set the cursor at the right of "mV="
lcd.print(millivolts); // Print the mV value
lcd.setCursor(4, 0); // Same here for °C and °F
lcd.print(celsius);
lcd.setCursor(4, 1);
Serial.print(farhenheit);
if (millivolts < 700) { // Green LED is on when the temperature is under or equal to 20°
// if (celsius < 20) { // Alternative
analogWrite(G_LED, 255);
analogWrite(Y_LED, 0); }
else {
analogWrite(G_LED, 0);
analogWrite(Y_LED, 255); // Yellow LED is on when the temperature is above of 20°C
}
delay(1000);
}
Fix - I could not find the error, but I suspect it was due to the strange layout of the connections. You also tried to set the cursor to line 3, but the LCD you were using did not have 3 lines, it was a 16x2 LCD.
What I Did - So what I did was I re-did the entire project, I linked up a new LCD, this time with a digital contrast so that it could be dynamically changed. I also made sure to include the sensor you used in your last project. Over-all the project is an Arduino controlling an LCD and outputting the temperature in Fahrenheit and millivolts.
Here is the project link (Tinkercad).
Code:
#include <LiquidCrystal.h>
// Adds the liquid crystal lib
int contrast = 40; // Set the contrast of the LCD
LiquidCrystal lcd (12, 11, 5, 4, 3, 2); // Instantiate the LCD
float fahrenheit;
float millivolts;
void setup ()
{
analogWrite(6, contrast); // Wrjte the contrast to the LCD
lcd.begin(16, 2); // Init the LCD
}
void loop ()
{
int sensor = analogRead(0);
millivolts = (sensor/1023.0)*5000;
fahrenheit = (((sensor*0.00488)-0.5)/0.01)*1.8+32;
lcd.setCursor(0, 0);
lcd.print("Temp(F):");
lcd.setCursor(11, 0);
lcd.print(fahrenheit);
lcd.setCursor(0, 1);
lcd.print("Volts(mV):");
lcd.setCursor(12, 1);
lcd.print(millivolts);
}
Diagram

Data transferred via RF 433MHz between two Arduinos doesn't show up on TFT LCD1.8 without ttyUSB Serial connection

The Transmitter is working fine. But the receiver isn't working without a ttyUSB serial connection. I mean whenever I disconnected the PC from the receiver it's not showing the DATA it's getting from the Transmitter. I want it to work without the PC with just external power. What possibly went wrong?
It shows "Not Connected" when I add an external power source disconnecting the USB plug from PC
/*
SimpleReceive
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 3 (default 11)
*/
#include <VirtualWire.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
//char temperatureChar[4];
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen=VW_MAX_MESSAGE_LEN;
const int buzzer = 2; //buzzer to arduino pin 2
void setup()
{
// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();
// clear the screen with a black background
TFTscreen.background(0, 0, 0);
// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
// set the font size
TFTscreen.setTextSize(1);
// write the text to the top left corner of the screen
TFTscreen.text("Temp :\n ", 5, 5);
// ste the font size very large for the loop
TFTscreen.setTextSize(2);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 2 as an output
// Initialize the IO and ISR
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if(vw_get_message(buf, &buflen)) //non-blocking
{
// set the font color
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text((char *)buf, 20, 20);
// wait for a moment
delay(1000);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.text((char *)buf, 20, 20);
tone(buzzer, 2000); // Send 1KHz sound signal...
delay(1000);
noTone(buzzer);
}
else {
// set the font color
TFTscreen.stroke(255, 255, 0);
TFTscreen.setTextSize(1);
// print the sensor value
TFTscreen.text("Not connected", 20, 20);
// wait for a moment
delay(500);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.setTextSize(1);
TFTscreen.text("Not connected", 20, 20);
}
}
Solved. The USB cable was actually working as an antenna (radio). When I disconnect it, it loses the connection.

Problems with interrupt arduino nano with NRF24le1

I am trying to build a wireless control of an LED Stripe.
For this I use 2 Arduino Nano's with a NRF24le1 chip to communicate with each other.
Arduino 1 serves as a controller / master and has 3 buttons and 1 fader for the choice of the respective light mode / blackout, as well as the to serve light intensity.
I would like the values ​​as 6 bytes (button1, button2, button3,
faderintensiät) and received on the 2nd Arduino.
Not constant values ​​are sent, only at change of values.
On the 2nd Nano should then be activated by interrupt flags, if an button press what and the current value from Fader Held so that the loop stays clear and there via the RGBW Led Stripe, ever after which flag is selected, can be iterated.
How can I attach the interrupt? On my Transmitter Adruino the stream is running
This is my code:
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define LED_PIN 3
#define LED_COUNT 228
uint8_t BRIGHTNESS = 60;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
RF24 wirelessSPI(7, 8); // CE, CSN
#define PinIRQ 3 // Arduino Uno, Mega und Nano: Pin 3
#define IRQ 1
const byte address[6] = "00001";
boolean light_on = false;
boolean mode_a_active = false;
boolean mode_b_active = false;
boolean receivedMessage = false;
int interruptcounter = 0;
uint8_t settings[6] = {0, 0, 0, 0, 0, 0}; //Saving the incoming data
void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
wirelessSPI.begin();
//wirelessSPI.setAutoAck(1); //new
//wirelessSPI.enableAckPayload(); //new
//wirelessSPI.maskIRQ(1,1,0); //new
wirelessSPI.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
wirelessSPI.openReadingPipe(0, address); //Setting the address at which we will receive the data
wirelessSPI.startListening(); //This sets the module as receiver
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
attachInterrupt(IRQ, incomingmessage, HIGH);
}
void loop()
{
wirelessSPI.read(&settings, sizeof(settings)); //Reading the data
if(receivedMessage == true){
Serial.println("messange on interupt received");
Serial.print("interruptcounter counts: ");
Serial.println(interruptcounter);
Serial.println();
Serial.print(settings[0]);
Serial.print(settings[1]);
Serial.print(settings[2]);
Serial.print(settings[3]);
Serial.print(settings[4]);
Serial.println(settings[5]);
receivedMessage = false;
}
}
void incomingmessage()
{
interruptcounter++;
while (wirelessSPI.available()) {
wirelessSPI.read(&settings, sizeof(settings)); //Reading the data
receivedMessage = true; // Variable setzen, dass eine neue Nachricht zur Auswertung bereit steht
}
}

"Too many arguments" error

I am building a laser that is to be controlled by a joystick. The joystick uses 2 servo motors to move the laser in the direction I want. I am using an arduino board that uses C++ coding.
I get a "too many arguments" error on my outputs.
#include <Servo.h>
const int servo1 = 11; // first servo
const int servo2 = 10; // second servo
const int joy1 = 5; // switch 1
const int joy2 = 4; // switch 2
const int joy3 = 3; // switch 3
const int joy4 = 2; // switch 4
int servoVal; // variable to read the value from the digital pin
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
// Read the horizontal joystick value (value between 0 and 180)
servoVal = digitalRead(joy1, joy2, joy3, joy4);
servoVal = map(servoVal, 0, 45, 135, 180); // scale it to use it with the servo (result between 0 and 180)
myservo2.write(servoVal); // sets the servo position according to the scaled value
// Read the horizontal joystick value (value between 0 and 180)
servoVal = digitalRead(joy1, joy2, joy3, joy4);
servoVal = map(servoVal, 0, 45, 135, 180); // scale it to use it with the servo (result between 0 and 180)
myservo1.write(servoVal); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(digitalRead(joy1, joy2, joy3, joy4));
Serial.print ("---");
Serial.print(digitalRead(joy1, joy2, joy3, joy4));
Serial.println ("----------------");
}
Would the joystick actually give a digital value or should have been an analogRead? Also I'm pretty sure you can only sample one item at a time with the digitalRead.

Using Arduino light sensor value to jump video on Processing

My project is to create a robot Arduino flower with two sensors: light and pressure, that opens and closes according to the values in those two sensors, and at the same time plays different videos through Processing according to those values.
I've been having a really hard time passing on the value from the analog sensors in Arduino too processing. This is what I have so far:
Arduino
#include <Servo.h>
Servo myservo;
int pressure = 0;
int light = 1;
int pre;
int val;
void setup()
{
Serial.begin(9600);
myservo.attach(2);
}
void loop() {
pre = analogRead(pressure);
pre = map(pre, 918, 1023, 255, 0);
val = analogRead(light);
val = map(val, 0, 255, 0, 127);
Serial.print(val, DEC);
/*if (val>50) {
Serial.print(1);
}
else {
Serial.print(0);
}*/
myservo.write(val);
}
Processing
import processing.video.*;
import processing.serial.*; //arduino
Serial myPort; // Create object from Serial class
int val;
Movie movie;
void setup() {
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
size(640, 360);
background(0);
movie = new Movie(this, "transit.mov");
movie.loop();
}
void movieEvent(Movie m) {
m.read();
}
void draw() {
val = myPort.read();
println(val);
//if (movie.available() == true) {
// movie.read();
//}
image(movie, 0, 0, width, height);
if (val==49) {
movie.jump(1);
}
}
For now I'm just trying to make the video react to the light sensor, but haven't been able. All I get from the Processing readings is 48. The motor reacts fine to the sensors.
What output do you get from a simpler Processing sketch like this? What output would you expect?
import processing.serial.*;
Serial myPort; // The serial port
void setup() {
// List all the available serial ports
println(Serial.list());
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
while (myPort.available() > 0) {
int inByte = myPort.read();
println(inByte);
}
}
Also, why is your map function in your Arduino code pre = map(pre, 918, 1023, 255, 0); using 250 and 0 as your lower bound and upper bound respectively? That seems backwards. Should it not read pre = map(pre, 918, 1023, 0, 255);?

Resources